0

Edit: Looks like I was confusing asynchronous I/O with asynchronous function. This answer on another question also helped me in learning. https://stackoverflow.com/a/6738602/1184321


I'm a bit confused on what makes callbacks not synchronous. So I was looking for some clarification on whether or not I need to explicitly denote callbacks in Node.js in order for them to be run asynchronously?

Take the example code:

start(function (err, resp) {
    console.log(resp);
});

function start(callback) {
    return isTrue(callback);
}

function isTrue(callback) {
    return callback(null, true)
}

Is the above example code functionally equivalent to:

start(function (err, resp) {
    console.log(resp);
});

function start(callback) {
    isTrue(function(err, resp){
        return callback(err, resp);
    });
}

function isTrue(callback) {
    return callback(null, true)
}

Can someone tell if all of these functions would be run asynchronously and if not could some point me in the direction where I can learn more about asynchronous callbacks? The difference between synchronous / asynchronous function calls seems sort of magical to me at this point.

EDIT:

Community
  • 1
  • 1
  • It's not callbacks themselves that are asynchronous. Function calls are always synchronous, but underlying services may not be. – Pointy Dec 23 '15 at 16:38
  • @Pointy My understanding is that when _something_ ends the Node event loop can do something else if it has to wait for a response. So in the above examples can Node find something else to do between each function being called or am I missing a bigger picture? –  Dec 23 '15 at 16:41
  • No, that's not how it works. It's *inside* system services where non-synchronous actions are launched. The JavaScript code itself is synchronous. Service completion (or progress) triggers events that cause callbacks to be invoked. – Pointy Dec 23 '15 at 16:45
  • So, you start an I/O operation to read from a file. The Node runtime makes the appropriate low-level system calls and then immediately returns to your code. That event loop completes. A millisecond or two later, the operating system finishes reading the first block of the file and signals the Node runtime. Node, in turn, invokes the callback you supplied when you started the operation. – Pointy Dec 23 '15 at 16:47
  • @Pointy okay thanks I think I was confusing asynchronous I/O with asynchronous functions. –  Dec 23 '15 at 16:58

3 Answers3

1

Besides the extra function wrapper, the 2 versions are identical.

They're both fully synchronous, although the function pattern(s) hint asynchronous execution due to the use of callbacks.

A minimal example of an asynchronous code is:

setTimeout(function() {
  console.log('Asynchronous!');
}, 1000);
console.log('Synchronous...');

This console.log in the setTimeout callback is called asynchronously - out of order, vs. the outer console.log which is called synchronously - in order.

The difference boils down to when (in terms of order of execution, not timing) does a function execute. Synchronous statements execute in the order they appear in source code while asynchronous statements (functions really...) execute out of order, when done operation completes.

Many Node objects expose asynchronous operations, mostly I/O related, that use an "errback" style callback function. Once the asynchronous part is done (reading a file, connecting a socket, etc...) the callback is called.

Amit
  • 45,440
  • 9
  • 78
  • 110
0

The both code blocks are same, and both are synchronous. Think about the callbacks just like a chain of function calls - all this things are sync unless one of the functions is asynchronous - this means it runs in a separate process, or thread or just delayed, and the callback is called not immediately but in a particular time later.

tenbits
  • 7,568
  • 5
  • 34
  • 53
0

You are mixing things up a little bit.

Callbacks are for telling : Hey, service! Execute this thang when you're finished.

The actual service being executed, it might be a service waiting for a hamster that has to complete a mile long maze. Or a simple return statement.

So defining callbacks doesn't mean your code will be async. Just means you are using a pattern called event-driven architecture. Those events might be synchronous or asynchronous.

For example, here is an asynchronous callback :

function onFinished(err, res){}

function start(callback){
    setTimeout(callback, 1000);
}

start(onFinished);

// Can do some sync work here

// But onFinished will be called somewhere asynchronously 

Here is a synchronous callback

function hello(err, res){ console.log(hello) }

function start(callback){
    console.log('started');
    callback();
}
Ludovic C
  • 2,855
  • 20
  • 40