Skip to content

Promise

David Fahlander edited this page May 20, 2014 · 38 revisions

Represents an ECMAScript 6 compliant Promise/A+ implementation.

Syntax

return new Promise(function (resolve, reject) {
    // Do something and call resolve / reject when done.
}).then(function (result) {
    // This code is called if resolve() was called in the Promise constructor
}).catch(function (error) {
    // This code is called if reject() was called in the Promise constructor, or
    // if an exception was thrown in either constructor or previous then() call.
}).finally(function () {
    // This code will be called no matter if an error occurred or not.
});

Description

Implementation of a Promise/A+ documented at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Methods

Static Methods

Static Properties

Events

Implementation Details

This implementation is a fork of promise-light (https://github.com/taylorhakes/promise-light) by https://github.com/taylorhakes - an A+ and ECMASCRIPT 6 compliant Promise implementation.

Modified by David Fahlander to be indexedDB compliant (See discussion: https://github.com/promises-aplus/promises-spec/issues/45).

This implementation will resolve/reject the promise directly without going through setTimeout(fn, 0) when it's not needed. The use of setTimeout(fn,0) is only needed if resolve() / reject() was called in same tick as the constructor. The behavior is still 100% Promise/A+ compliant and the caller of new Promise() can be certain that the promise wont be triggered the lines after constructing the promise.

This topic was also discussed in the following thread: https://github.com/promises-aplus/promises-spec/issues/45 and this implementation solves that issue.

See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Clone this wiki locally