namespace webdriver.promise

A promise implementation based on the CommonJS promise/A and promise/B proposals. For more information, see http://wiki.commonjs.org/wiki/Promises.

Functions

<T> all(arr)code »

Given an array of promises, will return a promise that will be fulfilled with the fulfillment values of the input array's values. If any of the input array's promises are rejected, the returned promise will be rejected with the same reason.

Parameters
arrArray<(T|webdriver.promise.Promise<T>)>

An array of promises to wait on.

Returns
webdriver.promise.Promise<Array<T>>

A promise that is fulfilled with an array containing the fulfilled values of the input array, or rejected with the same reason as the first rejected value.


asap(value, callback, opt_errback)code »

Invokes the appropriate callback function as soon as a promised value is resolved. This function is similar to webdriver.promise.when, except it does not return a new promise.

Parameters
value*

The value to observe.

callbackFunction

The function to call when the value is resolved successfully.

opt_errback?Function=

The function to call when the value is rejected.


captureStackTrace(name, msg, topFn)code »

Generates an error to capture the current stack trace.

Parameters
namestring

Error name for this stack trace.

msgstring

Message to record.

topFnFunction

The function that should appear at the top of the stack; only applicable in V8.

Returns
Error

The generated error.


checkedNodeCall(fn, var_args)code »

Wraps a function that expects a node-style callback as its final argument. This callback expects two arguments: an error value (which will be null if the call succeeded), and the success value as the second argument. The callback will the resolve or reject the returned promise, based on its arguments.

Parameters
fnFunction

The function to wrap.

var_args...?

The arguments to apply to the function, excluding the final callback.

Returns
webdriver.promise.Promise

A promise that will be resolved with the result of the provided function's callback.


consume(generatorFn, opt_self, var_args)code »

Consumes a GeneratorFunction. Each time the generator yields a promise, this function will wait for it to be fulfilled before feeding the fulfilled value back into next. Likewise, if a yielded promise is rejected, the rejection error will be passed to throw.

Example 1: the Fibonacci Sequence.

promise.consume(function* fibonacci() {
  var n1 = 1, n2 = 1;
  for (var i = 0; i < 4; ++i) {
    var tmp = yield n1 + n2;
    n1 = n2;
    n2 = tmp;
  }
  return n1 + n2;
}).then(function(result) {
  console.log(result);  // 13
});

Example 2: a generator that throws.

promise.consume(function* () {
  yield promise.delayed(250).then(function() {
    throw Error('boom');
  });
}).thenCatch(function(e) {
  console.log(e.toString());  // Error: boom
});
Parameters
generatorFnFunction

The generator function to execute.

opt_self?Object=

The object to use as "this" when invoking the initial generator.

var_args...*

Any arguments to pass to the initial generator.

Returns
webdriver.promise.Promise<?>

A promise that will resolve to the generator's final result.

Throws
TypeError

If the given function is not a generator.


controlFlow()code »

Returns
webdriver.promise.ControlFlow

The currently active control flow.


createFlow(callback)code »

Creates a new control flow. The provided callback will be invoked as the first task within the new flow, with the flow as its sole argument. Returns a promise that resolves to the callback result.

Parameters
callbackfunction(webdriver.promise.ControlFlow): ?

The entry point to the newly created flow.

Returns
webdriver.promise.Promise

A promise that resolves to the callback result.


<T> defer()code »

Creates a new deferred object.

Returns
webdriver.promise.Deferred<T>

The new deferred object.


delayed(ms)code »

Creates a promise that will be resolved at a set time in the future.

Parameters
msnumber

The amount of time, in milliseconds, to wait before resolving the promise.

Returns
webdriver.promise.Promise

The promise.


<TYPE, SELF> filter(arr, fn, opt_self)code »

Calls a function for each element in an array, and if the function returns true adds the element to a new array.

If the return value of the filter function is a promise, this function will wait for it to be fulfilled before determining whether to insert the element into the new array.

If the filter function throws or returns a rejected promise, the promise returned by this function will be rejected with the same reason. Only the first failure will be reported; all subsequent errors will be silently ignored.

Parameters
arr(Array<TYPE>|webdriver.promise.Promise<Array<TYPE>>)

The array to iterator over, or a promise that will resolve to said array.

fnfunction(this: SELF, TYPE, number, Array<TYPE>): ?(boolean|webdriver.promise.Promise<boolean>)

The function to call for each element in the array.

opt_self?SELF=

The object to be used as the value of 'this' within fn.


<T> fulfilled(opt_value)code »

Creates a promise that has been resolved with the given value.

Parameters
opt_value?T=

The resolved value.

Returns
webdriver.promise.Promise<T>

The resolved promise.


fullyResolved(value)code »

Returns a promise that will be resolved with the input value in a fully-resolved state. If the value is an array, each element will be fully resolved. Likewise, if the value is an object, all keys will be fully resolved. In both cases, all nested arrays and objects will also be fully resolved. All fields are resolved in place; the returned promise will resolve on value and not a copy.

Warning: This function makes no checks against objects that contain cyclical references:

var value = {};
value['self'] = value;
promise.fullyResolved(value);  // Stack overflow.
Parameters
value*

The value to fully resolve.

Returns
webdriver.promise.Promise

A promise for a fully resolved version of the input value.


isGenerator(fn)code »

Tests is a function is a generator.

Parameters
fnFunction

The function to test.

Returns
boolean

Whether the function is a generator.


isPromise(value)code »

Determines whether a value should be treated as a promise. Any object whose "then" property is a function will be considered a promise.

Parameters
value*

The value to test.

Returns
boolean

Whether the value is a promise.


<TYPE, SELF> map(arr, fn, opt_self)code »

Calls a function for each element in an array and inserts the result into a new array, which is used as the fulfillment value of the promise returned by this function.

If the return value of the mapping function is a promise, this function will wait for it to be fulfilled before inserting it into the new array.

If the mapping function throws or returns a rejected promise, the promise returned by this function will be rejected with the same reason. Only the first failure will be reported; all subsequent errors will be silently ignored.

Parameters
arr(Array<TYPE>|webdriver.promise.Promise<Array<TYPE>>)

The array to iterator over, or a promise that will resolve to said array.

fnfunction(this: SELF, TYPE, number, Array<TYPE>): ?

The function to call for each element in the array. This function should expect three arguments (the element, the index, and the array itself.

opt_self?SELF=

The object to be used as the value of 'this' within fn.


<T> rejected(opt_reason)code »

Creates a promise that has been rejected with the given reason.

Parameters
opt_reason*=

The rejection reason; may be any value, but is usually an Error or a string.

Returns
webdriver.promise.Promise<T>

The rejected promise.


setDefaultFlow(flow)code »

Changes the default flow to use when no others are active.

Parameters
flowwebdriver.promise.ControlFlow

The new default flow.

Throws
Error

If the default flow is not currently active.


when(value, opt_callback, opt_errback)code »

Registers an observer on a promised value, returning a new promise that will be resolved when the value is. If value is not a promise, then the return promise will be immediately resolved.

Parameters
value*

The value to observe.

opt_callback?Function=

The function to call when the value is resolved successfully.

opt_errback?Function=

The function to call when the value is rejected.

Returns
webdriver.promise.Promise

A new promise.

Compiler Constants

LONG_STACK_TRACESboolean

Whether to append traces of then to rejection errors.

Types

CancellationError

Error used when the computation of a promise is cancelled.

ControlFlow

Handles the execution of scheduled tasks, each of which may be an asynchronous operation.

Deferred

Represents a value that will be resolved at some point in the future.

Promise

Represents the eventual value of a completed operation.

Thenable

Thenable is a promise-like object with a then method which may be used to schedule callbacks on a promised value.