Promise.prototype.then
Says what to do with a promise's result.
Available since: ES2015
Syntax
promise.then(onFulfilled, onRejected)Parameters
| Parameter | Type | Description |
|---|---|---|
| onFulfilled | Function | A function that receives the promise's result. |
| onRejected | Function | Optional. 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:
42A chain: each then gets the previous result:
Promise.resolve(2)
.then((n) => n * 10)
.then((n) => n + 1)
.then((n) => console.log(n));Output:
21Notes
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
- Promise.prototype.catch — Handles a promise's error.
- Promise.prototype.finally — Runs an action after a promise — on success and on failure.
- await — Waits for a promise and returns its value.
In the tutorial
- Promises — Promises in JavaScript: promise states, then, catch, finally, creating a promise with new Promise, and chains.