I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Registrati per tradurlo tu.
The then()
method returns a Promise
. It takes two arguments: callback functions for the success and failure cases of the Promise
.
Syntax
p.then(onFulfilled, onRejected); p.then(function(value) { // fulfillment }, function(reason) { // rejection });
Parameters
onFulfilled
- A
Function
called when thePromise
is fulfilled. This function has one argument, the fulfillmentvalue
. onRejected
- A
Function
called when thePromise
is rejected. This function has one argument, the rejectionreason
.
Return value
A Promise
.
Description
As the then
and Promise.prototype.catch()
methods return promises, they can be chained — an operation called composition.
Examples
Using the then
method
var p1 = new Promise(function(resolve, reject) { resolve("Success!"); // or // reject ("Error!"); }); p1.then(function(value) { console.log(value); // Success! }, function(reason) { console.log(reason); // Error! });
Chaining
Because the then
method returns a Promise
, you can easily chain then
calls. Values that is not a Promise
returned from the onFulfilled
or onRejected
callback functions will be automatically wrapped in a resolved promise.
var p2 = new Promise(function(resolve, reject) { resolve(1); }); p2.then(function(value) { console.log(value); // 1 return value + 1; }).then(function(value) { console.log(value); // 2 }); p2.then(function(value) { console.log(value); // 1 });
You can also use chaining to implement one function with a Promise-based API on top of another such function.
function fetch_current_data() { // The fetch() API returns a Promise. This function // exposes a similar API, except the fulfillment // value of this function's Promise has had more // work done on it. return fetch("current-data.json").then((response) => { if (response.headers.get("content-type") != "application/json") { throw new TypeError(); } var j = response.json(); // maybe do something with j return j; // fulfillment value given to user of // fetch_current_data().then() }); }
If onFulfilled returns a promise, the return value of then
will be resolved/rejected by the promise.
function resolveLater(resolve, reject) { setTimeout(function () { resolve(10); }, 1000); } function rejectLater(resolve, reject) { setTimeout(function () { reject(20); }, 1000); } var p1 = Promise.resolve("foo"); var p2 = p1.then(function() { // Return promise here, that will be resolved to 10 after 1 second return new Promise(resolveLater); }); p2.then(function(v) { console.log("resolved", v); // "resolved", 10 }, function(e) { // not called console.log("rejected", e); }); var p3 = p1.then(function() { // Return promise here, that will be rejected with 20 after 1 second return new Promise(rejectLater); }); p3.then(function(v) { // not called console.log("resolved", v); }, function(e) { console.log("rejected", e); // "rejected", 20 });
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Promise.prototype.then' in that specification. |
Standard | Initial definition in an ECMA standard. |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Promise.prototype.then' in that specification. |
Draft |
Browser compatibility
To contribute to this compatibility data, please write a pull request against this file: https://github.com/mdn/browser-compat-data/blob/master/javascript/promise.json.
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Servo |
---|---|---|---|---|---|---|---|
Basic Support | 32.0 | (Yes) | 29.0 | No support | 19 | 7.1 | No support |
Feature | Android | Chrome for Android | Edge Mobile | Firefox for Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic Support | 4.4.4 | 32.0 | (Yes) | 29 | No support | (Yes) | 8.0 |