Promise.resolve() 是如何工作的?具体来说,它究竟是如何打开 thenables 的?
How does Promise.resolve() work under the hood? Specifically, how exactly does it unwrap thenables?
根据 MDN:
...if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state;
这激起了我的兴趣,所以我想看看是否有可能在纯 Javascript 中重新实现此功能。我最终得到的是:
function resolve(p){
if (
p !== null &&
(
typeof p === "object" ||
typeof p === "function"
) &&
typeof p.then === "function"
) {
return p.then(
function resolved(value){
return new Promise( function (resolve, reject) {
resolve(value);
});
},
function rejected(error){
return new Promise( function (resolve, reject) {
reject(error);
});
}
);
}
else {
return new Promise( function (resolve, reject) {
resolve(p);
});
}
}
所以我用各种值对此进行了测试:
var a = new Promise (resolve => resolve("hello"));
var b = "hello";
var c = {
then: onFulfilled => onFulfilled("hello")
};
var d = {
then: onFulfilled => {
onFulfilled("hello")
}
}
Promise.resolve(a); //Promise {<resolved>: "hello"}
Promise.resolve(b); //Promise {<resolved>: "hello"}
Promise.resolve(c); //Promise {<resolved>: "hello"}
Promise.resolve(d); //Promise {<resolved>: "hello"}
resolve(a); //Promise {<resolved>: "hello"}
resolve(b); //Promise {<resolved>: "hello"}
resolve(c); //Promise {<resolved>: "hello"}
resolve(d); //undefined -- uh oh.
很明显,因为 d
的 then
方法没有 return 任何东西,所以结果是未定义的...但是如果我正确地看这个,Promise.resolve() 正在以某种方式提取这个值?是自动拉出onFulfilled
函数调用的return值吗?还是我完全看错了?
Promise.resolve
不依赖于 .then()
对 thenable 值的调用的 return 值。它看起来类似于:
function resolve(v) {
return new Promise((resolve, reject) => {
if (Object(v) === v && typeof v.then === "function") {
v.then(resolve, reject);
} else {
resolve(v); // fulfill (no recursive resolution)
}
});
}
这通常被称为Promise
constructor antipattern, but it's warranted when we cannot trust the then
method. I recommend to take a look at how Promises/A+指定处理thenables。
根据 MDN:
...if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state;
这激起了我的兴趣,所以我想看看是否有可能在纯 Javascript 中重新实现此功能。我最终得到的是:
function resolve(p){
if (
p !== null &&
(
typeof p === "object" ||
typeof p === "function"
) &&
typeof p.then === "function"
) {
return p.then(
function resolved(value){
return new Promise( function (resolve, reject) {
resolve(value);
});
},
function rejected(error){
return new Promise( function (resolve, reject) {
reject(error);
});
}
);
}
else {
return new Promise( function (resolve, reject) {
resolve(p);
});
}
}
所以我用各种值对此进行了测试:
var a = new Promise (resolve => resolve("hello"));
var b = "hello";
var c = {
then: onFulfilled => onFulfilled("hello")
};
var d = {
then: onFulfilled => {
onFulfilled("hello")
}
}
Promise.resolve(a); //Promise {<resolved>: "hello"}
Promise.resolve(b); //Promise {<resolved>: "hello"}
Promise.resolve(c); //Promise {<resolved>: "hello"}
Promise.resolve(d); //Promise {<resolved>: "hello"}
resolve(a); //Promise {<resolved>: "hello"}
resolve(b); //Promise {<resolved>: "hello"}
resolve(c); //Promise {<resolved>: "hello"}
resolve(d); //undefined -- uh oh.
很明显,因为 d
的 then
方法没有 return 任何东西,所以结果是未定义的...但是如果我正确地看这个,Promise.resolve() 正在以某种方式提取这个值?是自动拉出onFulfilled
函数调用的return值吗?还是我完全看错了?
Promise.resolve
不依赖于 .then()
对 thenable 值的调用的 return 值。它看起来类似于:
function resolve(v) {
return new Promise((resolve, reject) => {
if (Object(v) === v && typeof v.then === "function") {
v.then(resolve, reject);
} else {
resolve(v); // fulfill (no recursive resolution)
}
});
}
这通常被称为Promise
constructor antipattern, but it's warranted when we cannot trust the then
method. I recommend to take a look at how Promises/A+指定处理thenables。