Promise.resolve()
method returns a
Promise
object that is resolved with a given value. If the value is a promise, that promise is returned; 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. This function flattens nested layers of promise-like objects (e.g. a promise that resolves to a promise that resolves to something) into a single layer.
Promise.resolve(value);
value
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.
静态
Promise.resolve
function returns a
Promise
that is resolved.
Promise.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
});
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.error(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
});
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Promise.resolve' in that specification. |
To contribute to this compatibility data, please write a pull request against this repository: https://github.com/mdn/browser-compat-data .
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
resolve()
|
Chrome 32 | Edge 12 | Firefox 29 | IE No | Opera 19 | Safari 8 | WebView Android 4.4.3 | Chrome Android 32 | Firefox Android 29 | Opera Android 19 | Safari iOS 8 | Samsung Internet Android 2.0 | nodejs 0.12 |
完整支持
不支持
Promise
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()