Previous: Refactoring With Tasks
Next: Task Modifiers
task()
When a task is performed, it runs the code in the async arrow task function
you passed into task()
. (If you're unfamiliar with async functions
and the await
keyword, Mozilla has a good introductory
guide).
await
keyword
Much of the power of Tasks is unleashed once you start making
use of the await
keyword within async functions.
The await
keyword, when used with a promise, lets you
pause execution of your task function until that promise resolves, at
which point the task function will continue running from where it
had paused.
This example demonstrates how you can await timeout(1000)
to pause execution for 1000 ms (one second). The timeout()
helper function, which ember-concurrency provides,
simply returns a promise that resolves after the specified number of milliseconds.
When you await
a promise, the await
expression
evaluates to the resolved value of the promise. In other words, you can
set a variable equal to a yielded promise, and when the promise resolves,
the task function will resume and the value stored into that variable will
be the resolved value of the promise.
If you await
a promise that rejects, the task function will
throw the rejected value (likely an exception object) from the point in
task function where the rejecting promise was awaited. This means you can
use try {} catch(e) {} finally {}
blocks, just as you would
for code that runs synchronously.
Please see the older guides for how to use ember-concurrency with older versions of Ember.
Previous: Refactoring With Tasks
Next: Task Modifiers