Promise.prototype.then()

Registers callbacks for the fulfillment and/or rejection of a promise.

Since ES2015 (ES6) Spec ↗

Syntax

promise.then(onFulfilled, onRejected)

Returns

Promise — A new promise resolved with the return value of the called handler.

Examples

Promise.resolve(2)
  .then(n => n * 3)
  .then(n => console.log(n));
Output
6
Promise.reject(new Error("bad"))
  .then(null, e => console.log("caught:", e.message));
Output
caught: bad
Promise.resolve(1)
  .then(n => Promise.resolve(n + 1))
  .then(n => console.log(n));
Output
2

Notes

- Returning a promise from a handler waits for it to settle (chaining). - Omitted handlers pass through fulfillment or rejection. - Handlers always run asynchronously, in a microtask.

Browser & runtime support

EnvironmentSince version
chrome 32
firefox 29
safari 8
edge 12
node 0.12

See also