jFactory > Reference > Traits > TraitTask
TraitTask
Registers Promises wrapped by JFactoryPromise
objects that will be expired and removed at Remove Phase.
Behavior
-
During a Phase Change,
$taskRemoveAll(<New Phase>)
is called, which stops and removes all Tasks with a Remove Phase equal to the new Phase. -
Before completing, a Phase awaits the Completion of all the Tasks registered in
myComponent.$.tasks
, including promises registered during the current Service State Handler. -
The jFactory methods create Tasks when they operate asynchronously.
See also JFactoryPromise
, TraitService
Registry
myComponent.$.tasks
Injected Methods
$task(registryId {string}, executorOrValue {Promise | function | *})
Returns: JFactoryPromise
Defers current Phase: This method registers a Task that blocks the resolution of the current Phase (if any) until the whole Task chain (including subpromises) is resolved.
Auto completed: The promise chain is completed and expired as soon as all subpromises are resolved. This behavior can be disabled by setting
anyPromiseOfTheChain.$chain.chainConfig.chainAutoComplete = false
before the promise chain is completed.
Auto removed: This Subscription is removed as soon as its Promise Chain is completed.
- Passing an existing Promise or Thenable:
myComponent.$task('myPromise', promise) .then(() => "ok")
- Passing a Promise Executor (promise creation)
myComponent.$task('myPromise', (resolve, reject) => setTimeout(resolve, 1000)) .then(() => "ok")
- Passing a Value (creates a resolved promise)
myComponent.$task('myPromise', 41) .then(value => value + 1)
$taskRemove(registryId {string}, reason {string} = "$taskRemove()")
Expires and removes the promise registered with the key
registryId
previously created by$task()
. An optional reason can be given for debugging the expired Promise Chain.
$taskRemoveAll(phase)
(Automatically called at Remove Phase)
Removes any promise previously created by
$task()
if their Remove Phase match the givenphase
.
$taskPromiseAll(autoComplete = false)
Returns a Promise that fulfills when all the registered tasks fulfill.
IfautoComplete
is true, the chainAutoComplete is enabled on all tasks.