为什么 setTimeout 函数是非阻塞的?
Why setTimeout function is non blocking?
如果 setTimeout 是同步的,为什么它是非阻塞的?如果不是主线程在哪个线程执行?
Why setTimeout function does not return a promise?
主要是因为它早于 Promises 被添加到 JS 语言中。
Is setTimeout synchronous or asynchronous?
它运行同步并排队一个函数到运行稍后。
why does setTimeout does not return a promise but a number?
它 returns 一个用于 clearTimeout
的数字。
Can we use await on setTimeout?
你只能await
一个承诺。
您可以从传递给 setTimeout
的回调函数中 wrap a call to setTimeout
in a promise 和 resolve
它。
await new Promise( resolve => setTimeout(() => {
// do whatever you like after 1s
// then:
resolve();
}, 1000);
超时函数是异步的,这就是它是非阻塞的原因。它在主线程上执行。
超时函数执行如下:
The callbacks of timers in JavaScript(setTimeout, setInterval) are
kept in the heap memory until they are expired. If there are any
expired timers in the heap, the event loop takes the callbacks
associated with them and starts executing them in the ascending order
of their delay until the timers queue is empty.
setTimeout 本质上是同步的。它的回调在定时器阶段由事件循环注册,将以异步方式执行。
如果 setTimeout 是同步的,为什么它是非阻塞的?如果不是主线程在哪个线程执行?
Why setTimeout function does not return a promise?
主要是因为它早于 Promises 被添加到 JS 语言中。
Is setTimeout synchronous or asynchronous?
它运行同步并排队一个函数到运行稍后。
why does setTimeout does not return a promise but a number?
它 returns 一个用于 clearTimeout
的数字。
Can we use await on setTimeout?
你只能await
一个承诺。
您可以从传递给 setTimeout
的回调函数中 wrap a call to setTimeout
in a promise 和 resolve
它。
await new Promise( resolve => setTimeout(() => {
// do whatever you like after 1s
// then:
resolve();
}, 1000);
超时函数是异步的,这就是它是非阻塞的原因。它在主线程上执行。
超时函数执行如下:
The callbacks of timers in JavaScript(setTimeout, setInterval) are kept in the heap memory until they are expired. If there are any expired timers in the heap, the event loop takes the callbacks associated with them and starts executing them in the ascending order of their delay until the timers queue is empty.
setTimeout 本质上是同步的。它的回调在定时器阶段由事件循环注册,将以异步方式执行。