The Promise
object is used for asynchronous computations. A Promise
represents a value which may be available now, or in the future, or never.
new Promise( /* executor */ function(resolve, reject) { ... } );
resolve
and reject
. The executor
function is executed immediately by the Promise implementation, passing resolve
and reject
functions (the executor is called before the Promise
constructor even returns the created object). The resolve
and reject
functions, when called, resolve or reject the promise, respectively. The executor normally initiates some asynchronous work, and then, once that completes, either calls the resolve
function to resolve the promise or else rejects it if an error occurred.A Promise
is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.
A Promise
is in one of these states:
A pending promise can either be fulfilled with a value, or rejected with a reason (error). When either of these options happen, the associated handlers queued up by a promise's then
method are called. (If the promise has already been fulfilled or rejected when a corresponding handler is attached, the handler will be called, so there is no race condition between an asynchronous operation completing and its handlers being attached.)
As the
and Promise.prototype.then()
methods return promises, they can be chained.Promise.prototype.catch()
Not to be confused with: Several other languages have mechanisms for lazy evaluation and deferring a computation, which they also call “promises” — e.g. Scheme. Promises in JavaScript represent processes which are already happening, which can be chained with callback functions. If you are looking to lazily evaluate an expression, consider the arrow function with no arguments: f = () => expression
to create the lazily-evaluated expression, and f()
to evaluate.
Note: A promise is said to be settled if it is either fulfilled or rejected, but not pending. You will also hear the term resolved used with promises — this means that the promise is settled, or it is locked into a promise chain. Domenic Denicola's States and fates contains more details about promise terminology.
Promise.length
Promise.prototype
Promise
constructor.Promise.all(iterable)
Promise.race(iterable)
Promise.reject(reason)
Promise
object that is rejected with the given reason.Promise.resolve(value)
Promise
object that is resolved with the given value. If the value is a thenable (i.e. has a then
method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value. Generally, if you don't know if a value is a promise or not, Promise.resolve(value)
it instead and work with the return value as a promise.Promise
prototypePromise.prototype.constructor
Promise
function by default.Promise.prototype.catch(onRejected)
Promise.prototype.then(onFulfilled, onRejected)
onFulfilled
or onRejected
is not a function).This small example shows the mechanism of a Promise
. The testPromise()
method is called each time the <button>
is clicked. It creates a promise that will be fulfilled, using window.setTimeout()
, to the promise count (number starting from 1) every 1-3 seconds, at random. The Promise()
constructor is used to create the promise.
The fulfillment of the promise is simply logged, via a fulfill callback set using p1.then()
. A few logs shows how the synchronous part of the method is decoupled from the asynchronous completion of the promise.
'use strict'; var promiseCount = 0; function testPromise() { var thisPromiseCount = ++promiseCount; var log = document.getElementById('log'); log.insertAdjacentHTML('beforeend', thisPromiseCount + ') Started (<small>Sync code started</small>)<br/>'); // We make a new promise: we promise a numeric count of this promise, starting from 1 (after waiting 3s) var p1 = new Promise( // The resolver function is called with the ability to resolve or // reject the promise function(resolve, reject) { log.insertAdjacentHTML('beforeend', thisPromiseCount + ') Promise started (<small>Async code started</small>)<br/>'); // This is only an example to create asynchronism window.setTimeout( function() { // We fulfill the promise ! resolve(thisPromiseCount); }, Math.random() * 2000 + 1000); } ); // We define what to do when the promise is resolved/fulfilled with the then() call, // and the catch() method defines what to do if the promise is rejected. p1.then( // Log the fulfillment value function(val) { log.insertAdjacentHTML('beforeend', val + ') Promise fulfilled (<small>Async code terminated</small>)<br/>'); }) .catch( // Log the rejection reason function(reason) { console.log('Handle rejected promise ('+reason+') here.'); }); log.insertAdjacentHTML('beforeend', thisPromiseCount + ') Promise made (<small>Sync code terminated</small>)<br/>'); }
This example is started by clicking the button. You need a browser that supports Promise
. By clicking the button several times in a short amount of time, you'll even see the different promises being fulfilled one after another.
Another simple example using Promise
and XMLHttpRequest
to load an image is available at the MDN GitHub promise-test repository. You can also see it in action. Each step is commented and allows you to follow the Promise and XHR architecture closely.
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Promise' in that specification. | Standard | Initial definition in an ECMA standard. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Promise' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Servo |
---|---|---|---|---|---|---|---|
Promise | 32.0 | (Yes) | 29.01 | No support | 19 | 7.12 | No support |
Promise.all | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Promise.prototype | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Promise.prototype.catch | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Promise.prototype.then | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Promise.race | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Promise.reject | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Promise.resolve | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Feature | Android | Chrome for Android | Edge Mobile | Firefox for Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Promise | 4.4.4 | 32.0 | (Yes) | 291 | No support | (Yes) | 8.02 |
Promise.all | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |
Promise.prototype | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |
Promise.prototype.catch | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |
Promise.prototype.then | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |
Promise.race | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |
Promise.reject | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |
Promise.resolve | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |
1. Constructor requires a new operator since version 37.
2. Constructor requires a new operator since version 10.
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise