Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs asynchronous confusion

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 < object.anArray.length; i++) {
        third(object.anArray[i]);
    };
}

function third(html) {
    // Parse html
}
first();
like image 828
mnort9 Avatar asked Feb 19 '23 19:02

mnort9


1 Answers

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.

like image 125
Michelle Tilley Avatar answered Feb 27 '23 13:02

Michelle Tilley