According to MDN:
...if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state;
This piqued my interest a bit, so I wanted to see if it was possible to re-implement this functionality in pure Javascript. What I ended up with is this:
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);
});
}
}
So I tested this with a variety of values:
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.
So obviously since d
's then
method doesn't return anything, the result is undefined... but if I am looking at this right, Promise.resolve() is extracting this value under the hood somehow? Is it automagically pulling out the return value of the onFulfilled
function call? Or am I looking at this completely wrong?