The Promise.resolve(value)
method returns a Promise
object that is resolved with the given value. If the value is a thenable (i.e. has a "then" method
), the returned promise will "follow" that thenable, adopting its eventual state; if the value was a promise, that object becomes the result of the call to Promise.resolve; otherwise the returned promise will be fulfilled with the value.
Promise.resolve(value); Promise.resolve(promise); Promise.resolve(thenable);
Promise
. Can also be a Promise
or a thenable to resolve.A Promise
that is resolved with the given value, or the promise passed as value, if the value was a promise object.
The static Promise.resolve
function returns a Promise
that is resolved.
Promise.resolve
methodPromise.resolve('Success').then(function(value) { console.log(value); // "Success" }, function(value) { // not called });
var p = Promise.resolve([1,2,3]); p.then(function(v) { console.log(v[0]); // 1 });
Promise
var original = Promise.resolve(33); var cast = Promise.resolve(original); cast.then(function(value) { console.log('value: ' + value); }); console.log('original === cast ? ' + (original === cast)); // logs, in order: // original === cast ? true // value: 33
The inverted order of the logs is due to the fact that the then
handlers are called asynchronously. See how then
works here.
// Resolving a thenable object var p1 = Promise.resolve({ then: function(onFulfill, onReject) { onFulfill('fulfilled!'); } }); console.log(p1 instanceof Promise) // true, object casted to a Promise p1.then(function(v) { console.log(v); // "fulfilled!" }, function(e) { // not called }); // Thenable throws before callback // Promise rejects var thenable = { then: function(resolve) { throw new TypeError('Throwing'); resolve('Resolving'); }}; var p2 = Promise.resolve(thenable); p2.then(function(v) { // not called }, function(e) { console.log(e); // TypeError: Throwing }); // Thenable throws after callback // Promise resolves var thenable = { then: function(resolve) { resolve('Resolving'); throw new TypeError('Throwing'); }}; var p3 = Promise.resolve(thenable); p3.then(function(v) { console.log(v); // "Resolving" }, function(e) { // not called });
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Promise.resolve' in that specification. | Standard | Initial definition in an ECMA standard. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Promise.resolve' in that specification. | Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 32 | Yes | 29 | No | 19 | 8 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | 4.4.4 | 32 | Yes | 29 | Yes | 8 | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve