bigyellowduck
bigyellowduck

Reputation: 21

Asynchronous vs synchronous in javascript functions. (Node.js)

I am currently going through the nodeschool.io tutorial on Javascript. This is a sample code from one of the solutions.

module.exports = function (dir, filterStr, callback) {

  fs.readdir(dir, function (err, list) {
    if (err)
      return callback(err)

    list = list.filter(function (file) {
      return path.extname(file) === '.' + filterStr
    })

    callback(null, list)
  })
}

From what I understand, javascript is a synchronous language. However, node.js uses asynchronous functions. In this code, isn't it possible for the callback to happen before the if statement. Therefore, is there even any reason to have functions that are different from the format

    function(params, callback){
      // one line data manipulation
      callback();
    }

Upvotes: 2

Views: 904

Answers (3)

Scott Sauyet
Scott Sauyet

Reputation: 50807

From what I understand, javascript is a synchronous language

I think you are misunderstanding something here. Javascript is a single-threaded language. But that does not make it synchronous. Nearly every environment in which in runs, including the current biggest ones, browsers and Node.js, provide global event queues for asynchronous processing.

If I understand your question right, I'm also guessing you're confusing levels here. The overall module that is exposed here is providing its own asynchronous behavior by accepting a callback that will only be called asynchronously. But this is built atop another asynchronous block, the call to fs.readdir. The callback to that one is the anonymous function that makes up the majority of your code sample. This callback contains the if-statement. The other one, passed in from the outside, and named callback, will happen in the same order it appears inside that inner function, that is, after the if-statement.

Upvotes: 1

Kosmas Papadatos
Kosmas Papadatos

Reputation: 1315

A proper async function will usually call the callback after a timeout expires or even an event is fired or a message is recieved, meaning that it won't keep the thread busy while waiting, leaving behind a promise. In your example the script will continue after you assign the callback, and will call it when the async function's work is done, which will most likely be after the rest of your code is done.

Upvotes: 0

brianjob
brianjob

Reputation: 360

node is single threaded, and event based. the i/o is what is done asynchronously. when you call fs.readdir(), you're passing it a function object as an argument. think of it as a function pointer in c++. fs.readdir() is going to kick off an asynchronous operation and pass that function object to an event handler that will trigger once the i/o operation has completed.

Now, it doesn't wait for the i/o operation to finish, it just continues to execute code until it completes and then it goes back to listening for events.

When it receives the event triggered when the i/o operation completes, it will run the code in the function object that it was passed.

Because of its single threaded nature, you don't have to worry about those types of race conditions.

Upvotes: 3

Related Questions