Why does the Promise object block rendering?

后端 未结 1 1448
梦毁少年i
梦毁少年i 2020-12-04 03:04

I was testing the Promise object and wrote some code that simulates a long running task that is synchronous. I was comparing Promise and setTimeout - see fiddle:

<         


        
相关标签:
1条回答
  • 2020-12-04 03:39

    while (Date.now() - nu < 1000) {} doesn't simulate a long running task that is syncronous. It is a long running task that is syncronous.

    Promises are a way to manage asynchronous code, not a way to make code asynchronous, nor a way to simulate multithreading.

    For that, you need to look at workers.

    • Web Workers for browsers.
    • Worker Threads for Node.js.
    0 讨论(0)
提交回复
热议问题