Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Promise.prototype.then

Says what to do with a promise's result.

Available since: ES2015

Syntax

promise.then(onFulfilled, onRejected)

Parameters

ParameterTypeDescription
onFulfilledFunctionA function that receives the promise's result.
onRejectedFunctionOptional. A function for the error; usually catch() is used instead.

Return value

PromiseA new promise with the function's result — so then() calls can be chained.

Examples

Getting the result:

Promise.resolve(21).then((n) => console.log(n * 2));

Output:

42

A chain: each then gets the previous result:

Promise.resolve(2)
  .then((n) => n * 10)
  .then((n) => n + 1)
  .then((n) => console.log(n));

Output:

21

Notes

The function in then() runs after the current code, not immediately, even if the promise is already fulfilled. If it returns a promise, the next then() waits for it.

See also

In the tutorial

  • Promises — Promises in JavaScript: promise states, then, catch, finally, creating a promise with new Promise, and chains.

Thanks for reading!

Donate to the site — we work hard for you.