0

I have looked into web and similar threads in stackoverflow, but i am really not understanding the difference between them and the scenarios where one would be useful compared to other.

Let me put my understanding in code wise.

function sum(a, b, cb){
    setImmediate(function(){
        cb(a + b)
    });
}

sum(10, 10, console.log);
console.log("Done")

function anotherSum(a,b,cb){
    process.nextTick(function(){
        cb(a + b);
    });
}

anotherSum(2, 3, console.log);

console.log("End")

Here using the process.nextTick/setImmediate, we are actually delaying the callback execution.

Output:

Done End 5 20

Both are delaying the execution of the callback, can anyone tell me the exact difference with some example.

Shane
  • 5,517
  • 15
  • 49
  • 79
  • Did you read the Node.js documentation? – SLaks Dec 31 '14 at 18:59
  • @SLaks: I have read too many documentation, but i am not getting things clearly. – Shane Dec 31 '14 at 19:00
  • What part of http://nodejs.org/docs/latest/api/process.html#process_process_nexttick_callback don't you understand? – SLaks Dec 31 '14 at 19:01
  • All i am seeing in the web is nextTick() typically runs before any other I/O events fire and setImmediate runs after I/O events. I am not really getting this part visually. – Shane Dec 31 '14 at 19:08

0 Answers0