Class RapidContext.Async

Async(promise, onCancelledopt)

A cancellable Promise that is backwards-compatible with MochiKit.Async.Deferred. These promises can be used either as a deferred or as a normal promise (recommended).

Instances of this class are instanceof MochiKit.Async.Deferred (and NOT instanceof Promise) due to backwards compatibility. Use RapidContext.Async.isPromise to check for a promise-compatible API.

Constructor Summary
Creates a new cancellable promise.
Method Summary
Registers a callback function to this promise.
Registers a fulfilled callback function to this promise.
Registers one or two callback functions to this promise.
Registers a reject callback function to this promise.
Cancels this promise and calls the registered cancellation handler.
Registers a reject callback function to this promise.
Registers a finalizer callback function to this promise.
Registers one or two callback functions to this promise.
(static)
Injects a CSS stylesheet to the current page.
(static)
Loads an image from a URL.
(static)
Checks if an object conforms to the Promise API.
(static)
Injects a JavaScript to the current page.
(static)
Returns a delayed value.
(static)
Performs an XmlHttpRequest to a URL.

Constructor

new Async(promise, onCancelledopt)

Creates a new cancellable promise. The cancellation callback must be a no-op if the action is already performed.

Parameters:
Name Type Attributes Description
promise {Promise|Deferred|function|Error|Object} the promise to wrap
onCancelled {function} <optional>
the cancellation callback function
Source:
RapidContext_Async.js, line 41

Methods

addBoth(callbackopt) → {Async}

Registers a callback function to this promise.

Parameters:
Name Type Attributes Description
callback {function} <optional>
a callback if promise either fulfilled or rejected
Returns:
{Async} this same promise
Deprecated:
Provided for `MochiKit.Async.Deferred` compatibility
Source:
RapidContext_Async.js, line 196

addCallback(callbackopt) → {Async}

Registers a fulfilled callback function to this promise.

Parameters:
Name Type Attributes Description
callback {function} <optional>
a callback if promise fulfilled
Returns:
{Async} this same promise
Deprecated:
Provided for `MochiKit.Async.Deferred` compatibility
Source:
RapidContext_Async.js, line 159

addCallbacks(onFulfilledopt, onRejectedopt) → {Async}

Registers one or two callback functions to this promise.

Parameters:
Name Type Attributes Description
onFulfilled {function} <optional>
a callback if promise fulfilled
onRejected {function} <optional>
a callback if promise rejected
Returns:
{Async} this same promise
Deprecated:
Provided for `MochiKit.Async.Deferred` compatibility
Source:
RapidContext_Async.js, line 146

addErrback(errback) → {Async}

Registers a reject callback function to this promise.

Parameters:
Name Type Description
errback {function} a callback if promise rejected
Returns:
{Async} this same promise
Deprecated:
Provided for `MochiKit.Async.Deferred` compatibility
Source:
RapidContext_Async.js, line 177

cancel()

Cancels this promise and calls the registered cancellation handler. No other callbacks will be triggered after the promise has been cancelled (except finalizer).

Source:
RapidContext_Async.js, line 131

catch(onRejected) → {Async}

Registers a reject callback function to this promise.

Parameters:
Name Type Description
onRejected {function} a callback if promise rejected
Returns:
{Async} a new promise that resolves to whatever value the callback function returned
Source:
RapidContext_Async.js, line 107

finally(onFinally) → {Async}

Registers a finalizer callback function to this promise. Note that the finalizer MAY NOT BE CALLED if the promise is cancelled.

Parameters:
Name Type Description
onFinally {function} a callback for promise resolved
Returns:
{Async} a new promise
Source:
RapidContext_Async.js, line 119

then(onFulfilledopt, onRejectedopt) → {Async}

Registers one or two callback functions to this promise.

Parameters:
Name Type Attributes Description
onFulfilled {function} <optional>
a callback if promise fulfilled
onRejected {function} <optional>
a callback if promise rejected
Returns:
{Async} a new promise that resolves to whatever value the callback functions return
Source:
RapidContext_Async.js, line 94

(static) css(url) → {Async}

Injects a CSS stylesheet to the current page.

Parameters:
Name Type Description
url {string} the stylesheet URL to load
Returns:
{Async} a promise that resolves with the DOM <link> element
Source:
RapidContext_Async.js, line 254

(static) img(url) → {Async}

Loads an image from a URL.

Parameters:
Name Type Description
url {string} the image URL to load
Returns:
{Async} a promise that resolves with the DOM <img> element
Source:
RapidContext_Async.js, line 243

(static) isPromise(value) → {boolean}

Checks if an object conforms to the Promise API.

Parameters:
Name Type Description
value {Object} the object to check
Returns:
{boolean} true if the object is a promise, or false otherwise
Source:
RapidContext_Async.js, line 29

(static) script(url) → {Async}

Injects a JavaScript to the current page.

Parameters:
Name Type Description
url {string} the script URL to load
Returns:
{Async} a promise that resolves with the DOM <script> element
Source:
RapidContext_Async.js, line 266

(static) wait(millis, valueopt) → {Async}

Returns a delayed value.

Parameters:
Name Type Attributes Description
millis {number} the number of milliseconds to wait
value {Object} <optional>
the value to resolve with
Returns:
{Async} a new promise that resolves with the value
Source:
RapidContext_Async.js, line 228

(static) xhr(url, optsopt) → {Async}

Performs an XmlHttpRequest to a URL.

Parameters:
Name Type Attributes Description
url {string} the URL to request
opts {Object} <optional>
the request options
Properties
Name Type Attributes Description
method {string} <optional>
the HTTP method (e.g. GET, POST...)
headers {Object} <optional>
the HTTP headers to send
timeout {number} <optional>
the timeout in milliseconds (default is 30s)
log {string} <optional>
the logging prefix, or null (defaults to request)
responseType {string} <optional>
the expected HTTP response (e.g. json)
body {string} <optional>
the HTTP request body to send
Returns:
{Async} a new promise that resolves with either the XHR object, or an error if the request failed
Source:
RapidContext_Async.js, line 303