API Docs for: 3.10.3
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 (fulfill) {
    fulfill('I am a fulfilled promise');
});

var rejected = new Y.Promise(function (fulfill, 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 fulfill and reject functions as parameters. This function is called synchronously.

Item Index

Methods

Properties

Methods

getStatus

() String

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

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