Why do Promise libraries use event loops?

后端 未结 3 901
天命终不由人
天命终不由人 2020-12-15 11:43

Considering the following JavaScript code:

var promise = new Promise();
setTimeout(function() {
    promise.resolve();
}, 10);

function foo() { }
promise.th         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-15 12:10

    Promises are all about cooperative multitasking.

    Pretty much the only method to achieve that is to use message based scheduling.

    Timers (usually with 0 delay) are simply used to post the task/message into message queue - yield-to-next-task-in-the-queue paradigm. So the whole formation consisting of small event handlers works and more frequently you yield - more smoothly all this work.

提交回复
热议问题