0

What would be the output of setImmediate and setTimeout in the following code:

console.log("11111");

setImmediate(function A(){
console.log("2222");
});

console.log("3333");


setTimeout(function B(){

    console.log("4444");

},0);

console.log("5555");

Output:

11111
3333
5555
4444
2222

when i change time in setTimeout time to 10, output :

11111
3333
5555
2222
4444

Can anybody explain me this behaviour?

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116

1 Answers1

0

The setImmediate() is used to break up long running operations and run a callback function immediately after the browser has completed other operations such as events and display updates.

The setTimeout() calls a function or evaluates an expression after a specified number of milliseconds. Which mean even if the browser has not completed yet, it is already working a function inside.

When you put 10 means, it will works only after 10 milliseconds which is most likely your browser has completed already.

Khay
  • 1,530
  • 13
  • 28