Async

Description

Utilities for working with Deferred, Promise, and other asynchronous processes.

Variables Summary

Functions Summary

Classes Summary

Variables

Public API

ERROR_TIMEOUT

Value passed to fail() handlers that have been triggered due to withTimeout()'s timeout

Functions

Public API

chain

Chains a series of synchronous and asynchronous (jQuery promise-returning) functions together, using the result of each successive function as the argument(s) to the next. A promise is returned that resolves with the result of the final call if all calls resolve or return normally. Otherwise, if any of the functions reject or throw, the computation is halted immediately and the promise is rejected with this halting error.

functions Array.<function(*)>
Functions to be chained
args nullable Array
Arguments to call the first function with
Returns: jQuery.Promise
A promise that resolves with the result of the final call, or rejects with the first error.
Public API

doInParallel

Executes a series of tasks in parallel, returning a "master" Promise that is resolved once all the tasks have resolved. If one or more tasks fail, behavior depends on the failFast flag:

  • If true, the master Promise is rejected as soon as the first task fails. The remaining tasks continue to completion in the background.
  • If false, the master Promise is rejected after all tasks have completed.

If nothing fails: (M = master promise; 1-4 = tasks; d = done; F = fail) M ------------d 1 >---d . 2 >------d . 3 >---------d . 4 >------------d

With failFast = false: M ------------F 1 >---d . . 2 >------d . . 3 >---------F . 4 >------------d

With failFast = true: -- equivalent to $.when() M ---------F 1 >---d . 2 >------d . 3 >---------F 4 >------------d (#4 continues even though master Promise has failed) (Note: if tasks finish synchronously, the behavior is more like failFast=false because you won't get a chance to respond to the master Promise until after all items have been processed)

To perform task-specific work after an individual task completes, attach handlers to each Promise before beginProcessItem() returns it.

Note: don't use this if individual tasks (or their done/fail handlers) could ever show a user- visible dialog: because they run in parallel, you could show multiple dialogs atop each other.

items non-nullable Array.<*>
beginProcessItem non-nullable function(*, number):Promise
failFast non-nullable boolean
Returns: $.Promise
Public API

doInParallel_aggregateErrors

Executes a series of tasks in parallel, saving up error info from any that fail along the way. Returns a Promise that is only resolved/rejected once all tasks are complete. This is essentially a wrapper around doInParallel(..., false).

If one or more tasks failed, the entire "master" promise is rejected at the end - with one argument: an array objects, one per failed task. Each error object contains:

  • item -- the entry in items whose task failed
  • error -- the first argument passed to the fail() handler when the task failed
items non-nullable Array.<*>
beginProcessItem non-nullable function(*, number):Promise
Returns: $.Promise
Public API

doSequentially

Executes a series of tasks in serial (task N does not begin until task N-1 has completed). Returns a "master" Promise that is resolved once all the tasks have resolved. If one or more tasks fail, behavior depends on the failAndStopFast flag:

  • If true, the master Promise is rejected as soon as the first task fails. The remaining tasks are never started (the serial sequence is stopped).
  • If false, the master Promise is rejected after all tasks have completed.

If nothing fails: M ------------d 1 >---d . 2 >--d . 3 >--d . 4 >--d

With failAndStopFast = false: M ------------F 1 >---d . . 2 >--d . . 3 >--F . 4 >--d

With failAndStopFast = true: M ---------F 1 >---d . 2 >--d . 3 >--F 4 (#4 never runs)

To perform task-specific work after an individual task completes, attach handlers to each Promise before beginProcessItem() returns it.

items non-nullable Array.<*>
beginProcessItem non-nullable function(*, number):Promise
failAndStopFast non-nullable boolean
Returns: $.Promise
Public API

doSequentiallyInBackground

Executes a series of synchronous tasks sequentially spread over time-slices less than maxBlockingTime. Processing yields by idleTime between time-slices.

items non-nullable Array.<*>
fnProcessItem non-nullable function(*, number)
Function that synchronously processes one item
maxBlockingTime optional number
idleTime optional number
Returns: $.Promise
Public API

waitForAll

Allows waiting for all the promises to be either resolved or rejected. Unlike $.when(), it does not call .fail() or .always() handlers on first reject. The caller should take all the precaution to make sure all the promises passed to this function are completed to avoid blocking.

If failOnReject is set to true, promise returned by the function will be rejected if at least one of the promises was rejected. The default value is false, which will cause the call to this function to be always successfully resolved.

If timeout is specified, the promise will be rejected on timeout as per Async.withTimeout.

promises non-nullable Array.<$.Promise>
Array of promises to wait for
failOnReject optional boolean
Whether to reject or not if one of the promises has been rejected.
timeout optional number
Number of milliseconds to wait until rejecting the promise
Returns: $.Promise
Promise which will be completed once al the
Public API

withTimeout

Adds timeout-driven failure to a Promise: returns a new Promise that is resolved/rejected when the given original Promise is resolved/rejected, OR is rejected after the given delay - whichever happens first.

If the original Promise is resolved/rejected first, done()/fail() handlers receive arguments piped from the original Promise. If the timeout occurs first instead, fail() is called with the token Async.ERROR_TIMEOUT.

promise $.Promise
timeout number
Returns: $.Promise

Classes

Constructor

PromiseQueue

Properties
Methods

Properties

Private

_curPromise

Private

_queue

Methods

Private

_doNext

add

Adds an operation to the queue. If nothing is currently executing, it will execute immediately (and the next operation added to the queue will wait for it to complete). Otherwise, it will wait until the last operation in the queue (or the currently executing operation if nothing is in the queue) is finished. The operation must return a promise that will be resolved or rejected when it's finished; the queue will continue with the next operation regardless of whether the current operation's promise is resolved or rejected.

op function(): $.Promise
The operation to add to the queue.

removeAll

Removes all pending promises from the queue.