Previous: Cancelation

Next: Child Tasks

Errors vs Cancelation

When you yield a promise, your task function will pause execution until one of three things happens:

  1. The promise fulfills, and your task will continue executing from that point.
  2. The promise rejects, and your task will automatically `throw` an error from that point.
  3. Something causes the task to be canceled, which has the behavior described below.

The Task Function Syntax docs demonstrates how you can use standard JavaScript try/catch blocks to catch exceptions thrown when you yield a rejecting promise, but what about cancelation? Are cancelations considered exceptions/errors, or something else?

In ember-concurrency, cancelation is considered a third kind of "completion" (the other two being a successful return from a function, and throwing an exception from a function). Specifically, this means that if a task is canceled while it is paused on a yield, the task will essentially return from that point, it will skip any catch(e) {} blocks it is in, but it will execute any finally {} blocks. The benefit of this behavior is that:

  1. finally blocks will always run and can be used for cleanup logic *
  2. You don't have to distinguish between cancelation and thrown exceptions in your catch blocks (which you'd annoyingly have to do if cancelation were considered just another type of error).

* While finally blocks are nice for cleanup logic, make sure you're leveraging the power of Task Modifiers and .isRunning / .isIdle task properties as much as possible so that you're not manually re-implementing a lot of the implicit state that ember-concurrency provides you for free, e.g. you should avoid manually toggling the visibility of a loading spinner within a task if you could accomplish the same thing using the .isRunning property on a task.

Example

Both of the buttons below will (re)start myTask when clicked. If you click the buttons quickly, it will cause the currently running task to cancel from the yield where it is paused. Notice how cancelations don't increment the numErrors property because cancelations skip the catch block.

  • Task State: idle
  • Completions: 0
  • Errors: 0
  • Finally block runs: 0
<button onclick={{perform myTask false}}>
  Run to Completion
</button>

<button onclick={{perform myTask true}}>
  Throw an Error
</button>

<ul>
  <li>Task State: {{myTask.state}}</li>
  <li>Completions: {{numCompletions}}</li>
  <li>Errors: {{numErrors}}</li>
  <li>Finally block runs: {{numFinallys}}</li>
</ul>
export default Controller.extend({
  numCompletions: 0,
  numErrors: 0,
  numFinallys: 0,

  myTask: task(function * (doError) {
    try {
      yield timeout(1000);
      if (doError) {
        throw new Error("Boom");
      }
    } catch(e) {
      this.incrementProperty('numErrors');
    } finally {
      this.incrementProperty('numFinallys');
    }
    this.incrementProperty('numCompletions');
  }).restartable(),
});




Previous: Cancelation

Next: Child Tasks