Promise.resolve()
Promise.resolve() 静态方法将给定的值“解析”为一个 Promise。如果该值为一个 Promise,则返回该 Promise;如果该值为一个 thenable,则 Promise.resolve() 会调用它准备好的两个回调函数来执行 then() 方法;否则,返回的 Promise 将以该值进行 fulfillment。
此函数会“展平”嵌套的类 Promise 对象(例如,一个解析为另一个解析为某个值的 Promise),将其简化为一层——一个解析为非 thenable 值的 Promise。
试一试
const promise1 = Promise.resolve(123);
promise1.then((value) => {
console.log(value);
// Expected output: 123
});
语法
Promise.resolve(value)
参数
value-
要由此
Promise解析的参数。也可以是 Promise 或 thenable 对象进行解析。
返回值
一个 Promise,它会以给定的值进行解析,或者,如果该值为一个 Promise 对象,则返回该 Promise。已解析的 Promise 可以处于任何状态——fulfilled(已成功)、rejected(已失败)或 pending(待定)。例如,解析一个已拒绝的 Promise 仍会得到一个已拒绝的 Promise。
描述
Promise.resolve()解析一个 Promise,这与 fulfillment 或 rejection(已成功或已失败)不同。有关术语的定义,请参阅 Promise 描述。简而言之,Promise.resolve() 返回的 Promise 的最终状态取决于另一个 Promise、thenable 对象或其他值。
注意: 如果对 value 表达式的求值可能会同步抛出错误,那么此错误不会被 Promise.resolve() 捕获并包装到已拒绝的 Promise 中。在这种情况下,请考虑使用 Promise.try(() => value)。
Promise.resolve() 是通用的,支持子类化,这意味着它可以被调用在 Promise 的子类上,并且结果将是子类类型的 Promise。要做到这一点,子类的构造函数必须实现与 Promise() 构造函数相同的签名——接受一个单独的 executor 函数,该函数可以作为参数传递 resolve 和 reject 回调。
Promise.resolve() 会特殊处理原生的 Promise 实例。如果 value 属于 Promise 或其子类,并且 value.constructor === Promise,那么 Promise.resolve() 将直接返回 value,而不会创建新的 Promise 实例。否则,Promise.resolve() 本质上是 new Promise((resolve) => resolve(value)) 的简写。
大部分的解析逻辑实际上是由 Promise() 构造函数传递的 resolve 函数实现的。总而言之:
- 如果传入一个非 thenable 的值,则返回的 Promise 已经使用该值 fulfilled。
- 如果传入一个 thenable,则返回的 Promise 将通过调用
then方法并传递一对解析函数作为参数来采纳该 thenable 的状态。(但是,由于原生 Promises 直接通过Promise.resolve()返回,而无需创建包装器,因此then方法不会对原生 Promises 调用。)如果resolve函数接收到另一个 thenable 对象,它将再次被解析,这样 Promise 的最终 fulfillment 值将永远不会是 thenable。
示例
使用静态 Promise.resolve 方法
Promise.resolve("Success").then(
(value) => {
console.log(value); // "Success"
},
(reason) => {
// not called
},
);
解析数组
const p = Promise.resolve([1, 2, 3]);
p.then((v) => {
console.log(v[0]); // 1
});
解析另一个 Promise
Promise.resolve() 会重用现有的 Promise 实例。如果它正在解析一个原生 Promise,它将返回相同的 Promise 实例,而不会创建包装器。
const original = Promise.resolve(33);
const cast = Promise.resolve(original);
cast.then((value) => {
console.log(`value: ${value}`);
});
console.log(`original === cast ? ${original === cast}`);
// Logs, in order:
// original === cast ? true
// value: 33
日志的倒序是由于 then 处理器是异步调用的。有关更多信息,请参阅 then() 参考。
解析 thenables 和抛出 Errors
// Resolving a thenable object
const p1 = Promise.resolve({
then(onFulfill, onReject) {
onFulfill("fulfilled!");
},
});
console.log(p1 instanceof Promise); // true, object casted to a Promise
p1.then(
(v) => {
console.log(v); // "fulfilled!"
},
(e) => {
// not called
},
);
// Thenable throws
// Promise rejects
const p2 = Promise.resolve({
then() {
throw new TypeError("Throwing");
},
});
p2.then(
(v) => {
// not called
},
(e) => {
console.error(e); // TypeError: Throwing
},
);
// Thenable throws after callback
// Promise resolves
const p3 = Promise.resolve({
then(onFulfilled) {
onFulfilled("Resolving");
throw new TypeError("Throwing");
},
});
p3.then(
(v) => {
console.log(v); // "Resolving"
},
(e) => {
// not called
},
);
嵌套的 thenables 将被“深度展平”为一个 Promise。
const thenable = {
then(onFulfilled, onRejected) {
onFulfilled({
// The thenable is fulfilled with another thenable
then(onFulfilled, onRejected) {
onFulfilled(42);
},
});
},
};
Promise.resolve(thenable).then((v) => {
console.log(v); // 42
});
警告: 不要对解析到自身的 thenable 调用 Promise.resolve()。这会导致无限递归,因为它试图展平一个无限嵌套的 Promise。
const thenable = {
then(onFulfilled, onRejected) {
onFulfilled(thenable);
},
};
Promise.resolve(thenable); // Will lead to infinite recursion.
在非 Promise 构造函数上调用 resolve()
Promise.resolve() 是一个通用方法。它可以被调用在任何实现了与 Promise() 构造函数相同签名的构造函数上。例如,我们可以调用它在一个将 console.log 作为 resolve 传递的构造函数上
class NotPromise {
constructor(executor) {
// The "resolve" and "reject" functions behave nothing like the
// native promise's, but Promise.resolve() calls them in the same way.
executor(
(value) => console.log("Resolved", value),
(reason) => console.log("Rejected", reason),
);
}
}
Promise.resolve.call(NotPromise, "foo"); // Logs "Resolved foo"
展平嵌套 thenables 的能力是由 Promise() 构造函数的 resolve 函数实现的,所以如果你在另一个构造函数上调用它,嵌套的 thenables 可能不会被展平,这取决于该构造函数如何实现其 resolve 函数。
const thenable = {
then(onFulfilled, onRejected) {
onFulfilled({
// The thenable is fulfilled with another thenable
then(onFulfilled, onRejected) {
onFulfilled(42);
},
});
},
};
Promise.resolve.call(NotPromise, thenable); // Logs "Resolved { then: [Function: then] }"
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-promise.resolve |
浏览器兼容性
加载中…