Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Nodejs asynchronous confusion

Nodejs asynchronous confusion

Problem

I can't seem to grasp how to maintain async control flow with NodeJs. All of the nesting makes the code very hard to read in my opinion. I'm a novice, so I'm probably missing the big picture.

What is wrong with simply coding something like this...

function first() {
    var object = {
        aProperty: 'stuff',
        anArray: ['html', 'html'];
    };
    second(object);
}

function second(object) {
    for (var i = 0; i 
Problem courtesy of: mnort9

Solution

The "big picture" is that any I/O is non-blocking and is performed asynchronously in your JavaScript; so if you do any database lookups, read data from a socket (e.g. in an HTTP server), read or write files to the disk, etc., you have to use asynchronous code. This is necessary as the event loop is a single thread, and if I/O wasn't non-blocking, your program would pause while performing it.

You can structure your code such that there is less nesting; for example:

var fs = require('fs');
var mysql = require('some_mysql_library');

fs.readFile('/my/file.txt', 'utf8', processFile);

function processFile(err, data) {
  mysql.query("INSERT INTO tbl SET txt = '" + data + "'", doneWithSql);
}

function doneWithSql(err, results) {
  if(err) {
    console.log("There was a problem with your query");
  } else {
    console.log("The query was successful.");
  }
}

There are also flow control libraries like async (my personal choice) to help avoid lots of nested callbacks.

You may be interested in this screencast I created on the subject.

Solution courtesy of: Michelle Tilley

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Nodejs asynchronous confusion

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×