小编LeC*_*ant的帖子

为什么无限循环会阻止承诺解决?

我对下面的代码有一个奇怪的问题:在函数调用之后添加一个无限循环将阻止在调用内部解决承诺。

我不明白为什么,以及这个循环如何影响承诺行为

const second_call = () => {
    return new Promise((resolve, reject) => {
        console.log("Second call");
        resolve();
    });
}

const first_call = () => {
    console.log("First call");
    second_call().then(() => {
        console.log("First call, THEN");
    });
}

const main = () => {
    console.log("Started");
    first_call();
    //if if comment the while (true), all debug msgs will be displayed
    //if i uncomment the while (true), the msg "First call, THEN" will not be displayed
    while (true);
}

main();
Run Code Online (Sandbox Code Playgroud)

asynchronous node.js promise

3
推荐指数
1
解决办法
752
查看次数

标签 统计

asynchronous ×1

node.js ×1

promise ×1