JavaScript works strangely in the situation below.
I saw this answer and got this question about Javascript's weird behavior: https://stackoverflow.com/a/50173415/1614973
I found that if we change the then
in his code to any other key name we will get a totally different result.
CodePen demo: his, changed
I tried Chrome and Firefox and they all have this problem. I explorer the problem there and find some basic rules of this "bug".
// This one will always pending
const pendingPromise = Promise.resolve(x=>x).then(r => ({ then: y => 0 }));
pendingPromise.then(r => console.log("promise resolved")); // "promise resolved" will never logged
// Thanks @Jaromanda X's correction. a simpler version is:
const pendingPromise1 = Promise.resolve().then(() => ({then: y => 0}))
pendingPromise1.then(r => console.log("promise1 resolved")); // "promise1 resolved" will never logged
The pendingPromise
is pending forever. There are three things to toggle this bug as far as I can see:
The original one must be fulfilled with a function.(unnecessary constraint)- In
.then
, an Object with a key of the name "then" must be returned. - The value of the key
then
must be a function.
I'd like to get any clue about why this happens.