Why use setTimeout in deferred

I tried to understand how deferred works, so in all of them they use setTimeout .

 this.callbacks;// array of functions reference this.callbacks.forEach(function(callback){ window.setTimeout(function(){ callback(data); },0); }); 

one example of these questions that use setTimeout

 resolve: function (data) { this.promise.okCallbacks.forEach(function(callback) { window.setTimeout(function () { callback(data) }, 0); }); }, 

What is the difference between calling functions in a loop on setTimeout than callback(); or callback.call();

+6
source share
4 answers

The reason for this is to allow the javascript stream to fire any other events that might wait in the queue.

Javascript is single threaded. If an event is triggered, it can only be executed when the current code completes.

Using setTimeout with a zero time delay effectively tells the JS interpreter that the callback function call is part of the new context and that your current code block is finished. This means that JS will seize the opportunity before calling callback() to check if any other events need to be processed.

Although this can lead to an immediate failure of callback() , it can be useful for the overall performance of your site:

Other events that may be required for processing include click events and other UI triggers. If you do not give them the opportunity to execute, this may result in your user interface being sluggish or unresponsive.

+10
source

setTimeout starts after a while, and the JavaScript process is free.

Using call will immediately call a callback and block everything else.

+2
source

setTimeout(func,0) not called immediately.

It just mimics an asynchronous function with a callback.

 function asyncFunc(callback) { console.log("step1"); setTimeout(callback,0); console.log("step2"); } asyncFunc(function() {console.log("async step")}); /* output: step1 step2 async step */ 

https://developer.mozilla.org/en-US/docs/Web/API/window.setTimeout

The code executed by setTimeout () is run in a separate execution context of the function with which it was called

Thus, it starts in a separate execution context, but before executing it, javascript must complete the current execution due to the fact that javascript runs on a single thread.

It doesn’t matter how much time is in the current execution, because it MUST end before performing another task.

if you have an infinite loop to the end of the task, for example, your callback will never be called.

+2
source

setTimeout() allows you to register a function that will be called once after a certain time.

The setTimeout() method of the Window object assigns a function to run after the specified number of milliseconds expire. setTimeout() returns a value that can be passed to clearTimeout() to cancel the execution of the scheduled function.

If you call setTimeout() with a time of 0 ms, the function you specified is not called immediately. Instead, it is placed in a queue that should be called "as soon as possible" after all the processed event handlers end.

+1
source

Source: https://habr.com/ru/post/952731/


All Articles