3

I started to develop in node.js just a while ago. Lately, I did some deep dive into the 'event loop' and async mechanism of node. But still I'm not fully understand the different between sync and async callbacks.

In this example from node.js API, I understand why is not clear which function will be called first.

maybeSync(true, () => {
  foo();
});
bar();

But, what if we had:

syncOrAsync(arg, () => {
 if (arg) {
   cb(arg);
   return;
 }
});

syncOrAsync(true, function(result) {
  console.log('result');
});

console.log('after result);

It's not clear to me why they are always execute in sync order, although I did a callback function which should execute by the event loop after the stack is empty ( console.log('after result') was finished ). Do I always need to add process.nextTick(cb); to get async? And what is the diffrent between process.nextTick and setTimeout();?

Community
  • 1
  • 1
wizard
  • 583
  • 2
  • 9
  • 26
  • 1
    Unless you have something that is actually async, like timers or external calls etc. the code will always be synchronous, as that's the default state of all javascript code, adding a callback doesn't make it asynchronous. – adeneo Nov 05 '16 at 10:28
  • So I always need to add `process.nextTick()`? – wizard Nov 05 '16 at 10:31
  • 1
    No, you need to add something that is async. – adeneo Nov 05 '16 at 10:32

1 Answers1

5

Unless you have something that is actually async, like timers or external calls etc. the code will always be synchronous, as that's the default state of all javascript code.
Adding a callback doesn't make it asynchronous

Here's an example of asynchronous code

function syncOrAsync(sync, cb) {
    if (sync) {
        return cb();
    } else {
        setTimeout(cb, 100); // async, waits 0.1 seconds to call callback
    }
}

syncOrAsync(true, function(result) { // synchronous call
    console.log('result 1'); // happens first
});

syncOrAsync(false, function(result) { // asynchronous call
    console.log('result 2'); // happens last, as it's async
});


console.log('result 3');     // happens second

Using process.nextTick() doesn't really make the functions asynchronous, but it does do somewhat the same

function syncOrAsync() {
    console.log('result 1'); // this happens last
}

process.nextTick(syncOrAsync);

console.log('result 2'); // this happens first

This would defer the execution of syncOrAsync until the next pass around the event loop, so it would be in many ways the same as setTimeout(syncOrAsync), but the function still wouldn't be asynchronous, the callback would execute immediately, we've just delay the entire execution of the function.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • So, if I understand you correct. The real power of async in node.js come into play when you are dealing with resources like database, files read, link requests etc. Which are taking a lot of I/O operations. The other stuff are working sync, unless you wrap them in `process.nextTick();` or `setTimeout(cb); ` – wizard Nov 05 '16 at 10:46
  • 2
    Yes, Javascript is synchronous and single-threaded, everything happens in the order it's written. Asynchronous code will most of the time rely on built in asynchronous functions, in the browser it's ajax, timers etc. and in Node it's calls to the OS, database lookup and things like that. – adeneo Nov 05 '16 at 10:55