This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
Summary
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; otherwise the returned promise will be fulfilled with the value.
Syntax
Promise.resolve(value); Promise.resolve(promise); Promise.resolve(thenable);
Parameters
- value
- Argument to be resolved by this
Promise. Can also be aPromiseor a thenable to resolve.
Description
The static Promise.resolve function returns a Promise that is resolved.
Examples
Using the static Promise.resolve method
Promise.resolve("Success").then(function(value) {
console.log(value); // "Success"
}, function(value) {
// not called
});
Resolving an array
var p = Promise.resolve([1,2,3]);
p.then(function(v) {
console.log(v[0]); // 1
});
Resolving another Promise
var original = Promise.resolve(true);
var cast = Promise.resolve(original);
cast.then(function(v) {
console.log(v); // true
});
Resolving thenables and throwing Errors
// 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
});
Specifications
| Specification | Status | Comment |
|---|---|---|
| domenic/promises-unwrapping | Draft | Standardization work is taking place here. |
| ECMAScript 6 (ECMA-262) The definition of 'Promise.resolve' in that specification. |
Release Candidate | Initial definition in an ECMA standard. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 32 [2] | 24.0 (24.0) as Future25.0 (25.0) as Promise behind a flag[1]29.0 (29.0) by default |
Not supported | 19 | 7.1 |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|
| Basic support | Not supported | 24.0 (24.0) as Future25.0 (25.0) as Promise behind a flag[1]29.0 (29.0) by default |
Not supported | Not supported | iOS 8 | 32 |
[1] Gecko 24 has an experimental implementation of Promise, under the initial name of Future. It got renamed to its final name in Gecko 25, but disabled by default behind the flag dom.promise.enabled. Bug 918806 enabled Promises by default in Gecko 29.
[2] Chrome/v8 also supports a non-standard method Promise.accept(). However, it is recommended to use this standard Promise.resolve() method instead as the accept method is going to be removed in the future (issue 3238).