Version 3.18.1
Show:

Promise Class

Module: promise

A promise represents a value that may not yet be available. Promises allow you to chain asynchronous operations, write synchronous looking code and handle errors throughout the process.

This constructor takes a function as a parameter where you can insert the logic that fulfills or rejects this promise. The fulfillment value and the rejection reason can be any JavaScript value. It's encouraged that rejection reasons be error objects


            var fulfilled = new Y.Promise(function (resolve) {
                resolve('I am a fulfilled promise');
            });
            
            var rejected = new Y.Promise(function (resolve, reject) {
                reject(new Error('I am a rejected promise'));
            });
            

Constructor

Promise

(
  • fn
)

Parameters:

  • fn Function

    A function where to insert the logic that resolves this promise. Receives resolve and reject functions as parameters. This function is called synchronously.

Item Index

Methods

Properties

Methods

_wrap

(
  • resolve
  • reject
  • fn
)
Function private

Wraps the callback in another function to catch exceptions and turn them into rejections.

Parameters:

  • resolve Function

    Resolving function of the resolver that handles this promise

  • reject Function

    Rejection function of the resolver that handles this promise

  • fn Function

    Callback to wrap

Returns:

all

(
  • values
)
static

Returns a promise that is resolved or rejected when all values are resolved or any is rejected. This is useful for waiting for the resolution of multiple promises, such as reading multiple files in Node.js or making multiple XHR requests in the browser.

Parameters:

  • values Any

    An array of any kind of values, promises or not. If a value is not

Returns:

[Promise] A promise for an array of all the fulfillment values

catch

(
  • [Function]
)
Promise

A shorthand for promise.then(undefined, callback).

Returns a new promise and the error callback gets the same treatment as in then: errors get caught and turned into rejections, and the return value of the callback becomes the fulfilled value of the returned promise.

Parameters:

  • [Function] Object optional

    errback Callback to be called in case this promise is rejected

Returns:

Promise:

A new promise modified by the behavior of the error callback

getStatus

() String deprecated

Returns the current status of the operation. Possible results are "pending", "fulfilled", and "rejected".

Returns:

isPromise

(
  • obj
)
Boolean static

Checks if an object or value is a promise. This is cross-implementation compatible, so promises returned from other libraries or native components that are compatible with the Promises A+ spec should be recognized by this method.

Parameters:

  • obj Any

    The object to test

Returns:

Boolean:

Whether the object is a promise or not

race

(
  • values
)
Promise static

Returns a promise that is resolved or rejected when any of values is either resolved or rejected. Can be used for providing early feedback in the UI while other operations are still pending.

Parameters:

  • values Any

    An array of values or promises

Returns:

reject

(
  • reason
)
Promise static

A shorthand for creating a rejected promise.

Parameters:

  • reason Any

    Reason for the rejection of this promise. Usually an Error Object

Returns:

Promise:

A rejected promise

resolve

(
  • Any
)
Promise static

Ensures that a certain value is a promise. If it is not a promise, it wraps it in one.

This method can be copied or inherited in subclasses. In that case it will check that the value passed to it is an instance of the correct class. This means that PromiseSubclass.resolve() will always return instances of PromiseSubclass.

Parameters:

  • Any Any

    object that may or may not be a promise

Returns:

then

(
  • [callback]
  • [errback]
)
Promise

Schedule execution of a callback to either or both of "fulfill" and "reject" resolutions for this promise. The callbacks are wrapped in a new promise and that promise is returned. This allows operation chaining ala functionA().then(functionB).then(functionC) where functionA returns a promise, and functionB and functionC may return promises.

Asynchronicity of the callbacks is guaranteed.

Parameters:

  • [callback] Function optional

    function to execute if the promise resolves successfully

  • [errback] Function optional

    function to execute if the promise resolves unsuccessfully

Returns:

Promise:

A promise wrapping the resolution of either "resolve" or "reject" callback

Properties

_resolver

Object private

A reference to the resolver object that handles this promise