Promise

by 10 contributors:

This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.

The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future.

Syntax

new Promise(executor);
new Promise(function(resolve, reject) { ... });

Parameters

executor
Function object with two arguments resolve and reject. The first argument fulfills the promise, the second argument rejects it. We can call these functions once our operation is completed.

Description

A Promise represents a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers to an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of the final value, the asynchronous method returns a promise of having a value at some point in the future.

A Promise is in one of these states:

  • pending: initial state, not fulfilled or rejected.
  • fulfilled: meaning that the operation completed successfully.
  • rejected: meaning that the operation failed.

A pending promise can become either fulfilled with a value, or rejected with a reason (error). When either of these happens, 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 Promise.prototype.then and Promise.prototype.catch methods return promises, they can be chained—an operation called composition.

Note:
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.

Properties

Promise.length
Length property whose value is 1 (number of constructor arguments).
Promise.prototype
Represents the prototype for the Promise constructor.

Methods

Promise.all(iterable)
Returns a promise that resolves when all of the promises in the iterable argument have resolved. This is useful for aggregating results of multiple promises together.
Promise.race(iterable)
Returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.
Promise.reject(reason)
Returns a Promise object that is rejected with the given reason.
Promise.resolve(value)
Returns a 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 want to know if a value is a promise or not - Promise.resolve(value) it instead and work with the return value as a promise.
Hide Sidebar
See Also
  1. Standard built-in objects
  2. Promise
  3. Properties
    1. Promise.prototype
  4. Method
    1. This is an experimental API that should not be used in production code. Promise.all()
    2. This is an experimental API that should not be used in production code. Promise.catch()
    3. This is an experimental API that should not be used in production code. Promise.then()
    4. This is an experimental API that should not be used in production code. Promise.race()
    5. This is an experimental API that should not be used in production code. Promise.reject()
    6. This is an experimental API that should not be used in production code. Promise.resolve()
  5. Inheritance:
  6. Function
    1. Properties
    2. Methods
  7. Object
    1. Properties
    2. Methods