Node: Asynchronous API for non-IO

1、setTimeout()、setInterval()

It is consistent with the browser's api, which is a single and multiple timed execution tasks.

The principle of implementation is similar to that of asynchronous IO, which does not require the participation of the IO thread pool.

 

2、process.nextTick()

In order to execute a task asynchronously immediately without being in the event queue, wait for the event loop to poll to

setTimeout(fn,0) can be done, but it is a waste of performance. A more efficient option is to use process.nextTick().

Each time the process.nextTick() method is called, only the callback function will be put into the queue and taken out for execution in the next round of Tick.

eg:

process.nextTick(function(){

        console.log("ssss");

})

 

3、setImmediate()

Very similar to the process.nextTick() method. But the priority is lower than process.nextTick()

This is due to the priority order of the observers: on each poll check,

idle observer > IO observer > check observer

process, nextTick() belongs to idle observer, setImmediate() belongs to check observer

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324935109&siteId=291194637