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 |
瀏覽器相容性
載入中…