Below is the example code:(下面是示例代码:)
var promise1 = new Promise(function(resolve, reject) {
console.log("Hi")
resolve("World")
}).then(function(value) {
console.log(value);
});
console.log("dummy");
console.log("Hello");
the output is(输出是)
Hi(你好)
dummy(假)
Hello(你好)
World(世界)
I'm a little bit confused, because resolve("World")
was executed before console.log("dummy");
(我有点困惑,因为resolve("World")
是在console.log("dummy");
之前执行的console.log("dummy");
) and console.log("Hello");
(和console.log("Hello");
) , since the promise is resolved, then the function in then
clause should be called immediately.(,由于承诺已解决,因此then
子句中的函数应立即调用。) so it should be more sense if the output is:(因此,如果输出为:)
Hi(你好)
World(世界)
dummy(假)
Hello(你好)
or the function might execute between last two statements, therefore the output is:(否则函数可能在最后两个语句之间执行,因此输出为:)
Hi(你好)
dummy(假)
World(世界)
Hello(你好)
so why no matter how many time I tried, the output is always the first case?(那么为什么不管我尝试多少次,输出总是第一种情况?)
ask by amjad translate from so