Class goog.Promise<TYPE, RESOLVER_CONTEXT>

Provided By
All Implemented Interfaces

NOTE: This class was created in anticipation of the built-in Promise type being standardized and implemented across browsers. Now that Promise is available in modern browsers, and is automatically polyfilled by the Closure Compiler, by default, most new code should use native Promise instead of goog.Promise. However, goog.Promise has the concept of cancellation which native Promises do not yet have. So code needing cancellation may still want to use goog.Promise.

Promises provide a result that may be resolved asynchronously. A Promise may be resolved by being fulfilled with a fulfillment value, rejected with a rejection reason, or blocked by another Promise. A Promise is said to be settled if it is either fulfilled or rejected. Once settled, the Promise result is immutable.

Promises may represent results of any type, including undefined. Rejection reasons are typically Errors, but may also be of any type. Closure Promises allow for optional type annotations that enforce that fulfillment values are of the appropriate types at compile time.

The result of a Promise is accessible by calling then and registering onFulfilled and onRejected callbacks. Once the Promise is settled, the relevant callbacks are invoked with the fulfillment value or rejection reason as argument. Callbacks are always invoked in the order they were registered, even when additional then calls are made from inside another callback. A callback is always run asynchronously sometime after the scope containing the registering then invocation has returned.

If a Promise is resolved with another Promise, the first Promise will block until the second is settled, and then assumes the same result as the second Promise. This allows Promises to depend on the results of other Promises, linking together multiple asynchronous operations.

This implementation is compatible with the Promises/A+ specification and passes that specification's conformance test suite. A Closure Promise may be resolved with a Promise instance (or sufficiently compatible Promise-like object) created by other Promise implementations. From the specification, Promise-like objects are known as "Thenables".

new Promise<TYPE, RESOLVER_CONTEXT>( resolver, opt_context )

resolverfunction(this: (RESOLVER_CONTEXT|null), function(()): ?, function(*): ?): undefined

Initialization function that is invoked immediately with resolve and reject functions as arguments. The Promise is resolved or rejected with the first argument passed to either function.


An optional context for executing the resolver function. If unspecified, the resolver function will be executed in the default scope.

See Also

Instance Methods

Static Functions

Compiler Constants