Promise.prototype.then()
Registers callbacks for the fulfillment and/or rejection of a promise.
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
| Environment | Since version |
|---|---|
| chrome | 32 |
| firefox | 29 |
| safari | 8 |
| edge | 12 |
| node | 0.12 |