Nodejs Asynchronous Confusion

I cannot figure out how to maintain an asynchronous control flow using NodeJs. I believe that all nesting makes the code very difficult to read. I am new, so I probably don’t see the big picture.

What's wrong just by encoding 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(); 
+4
source share
2 answers

The "big picture" is that any I / O is not blocked and runs asynchronously in your JavaScript; therefore, if you search databases, read data from a socket (for example, on an HTTP server), read or write files to disk, etc., you must use asynchronous code. This is necessary because the event loop is a single thread, and if I / O was not non-blocking, your program paused during execution.

You can structure your code so that there is less nesting; eg:

 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 such as async (my personal choice) to avoid a lot of nested callbacks.

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

+7
source

As @BrandonTilley said, I / O is asynchronous, so you need callbacks in Node.js to handle them. This is why Node.js can do so much with just one thread (it doesn’t actually do more in one thread, but instead of having the thread go around the data, it just starts processing the next task and when I / O returns, then it returns to this task using the callback function that you gave it).

But nested callbacks can be taken care of with a good library, such as the venerable async or my new little library: queue-flow . They handle callback problems and allow you to keep the code non-nested and similar to blocking, synchronous code. :)

+1
source

All Articles