Deferred

class Deferred.Failure(value)

A Deferred.Deferred() failure result.

This class may be inherited in order to add more attributes.

Deferred.Failure.Deferred.Failure.value

Value of failure

class Deferred.Deferred(canceller)
Arguments
  • canceller (function()) – Function to call on cancellation

Deferred is a design pattern where an object acts as a proxy for a future asynchronous event.

On Deferred instance, user may attach an arbitrary set of callback functions. They will be called in order, each one with return value of preceding callback.

Return value of each callback can be a Deferred.Failure() instance, indicating failure, or another object to indicate success.

Deferred pattern is one of the multiple ways to handle asynchronicity.

Some of its main advantages over basic continuations:

  • There is no need to pass continuation function references around when wrapping code. This makes functions with default parameters and variadic functions easier to write.

  • There can be multiple callbacks registered on completion or failure.

  • Transformation callbacks can be chained.

  • One can write code waiting from multiple deferreds before going on.

.import fbx.async 1.0 as Async

var x = new Async.Deferred.Deferred().then(function (value) {
    return value + " bar";
}).then(function (value) {
    console.log(value)
});

x.resolve("foo"); // will log "foo bar"
Deferred.Deferred.Deferred.Deferred.then(callback, errback)
Arguments
  • callback (function()) – Function to call when a successful value is to handle

  • errback (function()) – Function to call when a failure is to handle

Returns

this

Attach callback and/or errback to deferred. If deferred is resolved already, callback or errback will be called immediately.

Deferred.Deferred.Deferred.Deferred.fail(errback)

Alias for then(undefined, errback)

Deferred.Deferred.Deferred.Deferred.thenCall(cb)

Alias for then(function (ret) { cb(ret); return ret; })

i.e. insert a callback without value modification.

Deferred.Deferred.Deferred.Deferred.thenReturn(value)

Alias for then(function () { return value; })

i.e. insert a callback overriding value.

Deferred.Deferred.Deferred.Deferred.failCall(cb)

Alias for fail(function (ret) { cb(ret); return ret; })

i.e. insert a errback without value modification.

Deferred.Deferred.Deferred.Deferred.failReturn(value)

Alias for fail(function () { return value; })

i.e. insert a errback overriding value.

Deferred.Deferred.Deferred.Deferred.both(callback)

Alias for then(callback, callback)

i.e. insert a callback always called.

Deferred.Deferred.Deferred.Deferred.resolve(result)
Arguments
  • result (variant()) – Initial resolution value of Deferred.

Mark the deferred as resolved and start calling handler chain. If result is an instance of Deferred.Failure(), result is considered an error and errback is called.

Deferred.Deferred.Deferred.Deferred.reject(result)

Equivalent of calling resolve() with a failure. If result is not a failure, result will be wrapped in a Deferred.Failure() instance.

Deferred.Deferred.Deferred.Deferred.cancel()

Cancels deferred, subsequent calls to resolve() or fail() are forbidden.

Deferred.Deferred.Deferred.resolved(value)

A static method

Arguments
  • value (variant()) – Resolution value

Returns

a new deferred, resolved with value

Alias for new Deferred.Deferred().resolve(value)

Deferred.Deferred.Deferred.rejected(value)

A static method

Arguments
  • value (variant()) – Resolution value

Returns

a new deferred, rejected with value

Alias for new Deferred.Deferred().reject(value)

class Deferred.List(deferreds)
Arguments

A special type of deferred waiting for all passed deferreds. Result passed to first callback will be a list of all values from all deferreds, in order.

Deferred.List.Deferred.List.concatenated(lod)

A static method

Arguments
Returns

A new Deferred.Deferred()

Return deferred will be resolved when all from lod are. Result value will be a concatenation of all returned lists.