114

I've tried to research on how exactly asynchronous functions should be written. After a lot of plowing through a lot of documentation, it's still unclear to me.

How do I write asynchronous functions for Node? How should I implement error event handling correctly?

Another way to ask my question would be this: How should I interpret the following function?

var async_function = function(val, callback){
    process.nextTick(function(){
        callback(val);
    });
};

Also, I found this question on SO ("How do I create a non-blocking asynchronous function in node.js?") interesting. I don't feel like it has been answered yet.

9
  • 14
    That's why I'm asking. It's not apparent to me how these functions are any different.
    – Kriem
    Aug 1, 2011 at 13:26
  • I recommend you look at setTimeout and setInterval in your favourite browser and play around with them as well. Or ajax callbacks (probably the closest thing to the node experience), or event listeners for things you're familiar with like click and load events. The asynchronous model exists already in the browser, and they're exactly the same in node.
    – davin
    Aug 1, 2011 at 13:33
  • @davin - Guess I don't fully comprehend the asynchronous model then.
    – Kriem
    Aug 1, 2011 at 13:47
  • @Kriem, I answered something yesterday that might help: stackoverflow.com/questions/6883648/… It's not an answer to your question, but it's on-topic. Try and read the question and answer there and play around with the code to try and understand what is going on.
    – davin
    Aug 1, 2011 at 13:52
  • 2
    @Raynos What is the definition of "asynchronous function"? Oct 2, 2012 at 17:10

6 Answers 6

86

You seem to be confusing asynchronous IO with asynchronous functions. node.js uses asynchronous non-blocking IO because non blocking IO is better. The best way to understand it is to go watch some videos by ryan dahl.

How do I write asynchronous functions for Node?

Just write normal functions, the only difference is that they are not executed immediately but passed around as callbacks.

How should I implement error event handling correctly

Generally API's give you a callback with an err as the first argument. For example

database.query('something', function(err, result) {
  if (err) handle(err);
  doSomething(result);
});

Is a common pattern.

Another common pattern is on('error'). For example

process.on('uncaughtException', function (err) {
  console.log('Caught exception: ' + err);
});

Edit:

var async_function = function(val, callback){
    process.nextTick(function(){
        callback(val);
    });
};

The above function when called as

async_function(42, function(val) {
  console.log(val)
});
console.log(43);

Will print 42 to the console asynchronously. In particular process.nextTick fires after the current eventloop callstack is empty. That call stack is empty after async_function and console.log(43) have run. So we print 43 followed by 42.

You should probably do some reading on the event loop.

5
  • I've seen the Dahl vids, but I don't seem to get a grasp on the matter I'm afraid. :(
    – Kriem
    Aug 1, 2011 at 13:47
  • 1
    @Kriem see updated answer and read about the event loop
    – Raynos
    Aug 1, 2011 at 14:30
  • 1
    Thanks fort the insights. I'm now more aware of what I lack in knowledge. :) Your last example helped by the way.
    – Kriem
    Aug 1, 2011 at 18:13
  • I think you're statement about asynchronous IO is "better" is too general. In this sense yes, but overall that may not be the case.
    – Jake B
    Dec 19, 2014 at 16:37
  • In your first code example, you check the err argument, but didn't return afterward. In the case of an error, the code will continue and potentially cause serious problems in your application. Jan 5, 2016 at 22:15
9

Just passing by callbacks is not enough. You have to use settimer for example, to make function async.

Examples: Not async functions:

function a() {
  var a = 0;    
  for(i=0; i<10000000; i++) {
    a++;
  };
  b();
};

function b() {
  var a = 0;    
  for(i=0; i<10000000; i++) {
    a++;
  };    
  c();
};

function c() {
  for(i=0; i<10000000; i++) {
  };
  console.log("async finished!");
};

a();
console.log("This should be good");

If you will run above example, This should be good, will have to wait untill those functions will finish to work.

Pseudo multithread (async) functions:

function a() {
  setTimeout ( function() {
    var a = 0;  
    for(i=0; i<10000000; i++) {
      a++;
    };
    b();
  }, 0);
};

function b() {
  setTimeout ( function() {
    var a = 0;  
    for(i=0; i<10000000; i++) {
      a++;
    };  
    c();
  }, 0);
};

function c() {
  setTimeout ( function() {
    for(i=0; i<10000000; i++) {
    };
    console.log("async finished!");
  }, 0);
};

a();
console.log("This should be good");

This one will be trully async. This should be good will be writen before async finished.

0
5

You should watch this: Node Tuts episode 19 - Asynchronous Iteration Patterns

It should answers your questions.

0
3

If you KNOW that a function returns a promise, i suggest using the new async/await features in JavaScript. It makes the syntax look synchronous but work asynchronously. When you add the async keyword to a function, it allows you to await promises in that scope:

async function ace() {
  var r = await new Promise((resolve, reject) => {
    resolve(true)
  });

  console.log(r); // true
}

if a function does not return a promise, i recommend wrapping it in a new promise that you define, then resolve the data that you want:

function ajax_call(url, method) {
  return new Promise((resolve, reject) => {
    fetch(url, { method })
    .then(resp => resp.json())
    .then(json => { resolve(json); })
  });
}

async function your_function() {
  var json = await ajax_call('www.api-example.com/some_data', 'GET');
  console.log(json); // { status: 200, data: ... }
}

Bottom line: leverage the power of Promises.

1
  • Thing to remember here is, body of promise is still executed synchronously.
    – ns15
    Aug 13, 2019 at 11:10
2

Try this, it works for both node and the browser.

isNode = (typeof exports !== 'undefined') &&
(typeof module !== 'undefined') &&
(typeof module.exports !== 'undefined') &&
(typeof navigator === 'undefined' || typeof navigator.appName === 'undefined') ? true : false,
asyncIt = (isNode ? function (func) {
  process.nextTick(function () {
    func();
  });
} : function (func) {
  setTimeout(func, 5);
});
3
  • 18
    4 downvotes and not even one constructive comment.. :\
    – Omer
    Feb 8, 2015 at 14:44
  • 6
    @Omer Such is life on SO. Apr 30, 2016 at 1:45
  • 6
    @NorbertoBezi Maybe the code is self explanatory to you, but not to the one who posted the answer. That's why it's always a good practice to explain upon downvoting.
    – Omer
    May 22, 2016 at 12:18
0

I've dealing too many hours for such task in for node.js. I'm mainly front-end guy.

I find this quite important, because all node methods asyncronous deal with callback, and transform it into Promise is better to handle it.

I Just want to show a possible outcome, more lean and readable. Using ECMA-6 with async you can write it like this.

 async function getNameFiles (dirname) {
  return new Promise((resolve, reject) => {
    fs.readdir(dirname, (err, filenames) => {
      err !== (undefined || null) ? reject(err) : resolve(filenames)
    })
  })
}

the (undefined || null) is for repl (read event print loop) scenarios, using undefined also work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.