Synchronous flow in loops in nodejs

Problem

I have the following code for accepting input from command line in nodejs

for(i=0; i<2;i++)
{
    for(j=0; j<2; j++)
    {
        console.log("Enter data for "+i+"th row and "+j+"th column");
        prompt.get(['var1', 'var2'], function (error, result) {
             console.log("var1 is "+result.var1);
             console.log("var2 is "+result.var2);
        });
    }
}

I am getting the output as

Enter data for 0th row and 0th column
prompt: var1:  
Enter data for 0th row and 1th column
prompt: var1:  
Enter data for 1th row and 0th column
prompt: var1:
Enter data for 1th row and 1th column
prompt: var1:

As you can see the flow is getting synchronous for first iter's var1 and var2 but not synchronous between first iter's var2 and second iter's var1

I desire the following sequence of output where the first iter's var2 and second iter's var1 is also synchronous and so on.

Enter data for 0th row and 0th column
prompt: var1:  10
prompt: var2:  100
Enter data for 0th row and 1th column
prompt: var1:  20
prompt: var2:  100
Enter data for 1th row and 0th column
prompt: var1:  30
prompt: var2:  100
Enter data for 1th row and 1th column
prompt: var1:  40
prompt: var2:  100

Is there a way to do this in nodejs ?

Problem courtesy of: nik7

Solution

Full disclosure, I personally know the author of futures.

Have a look at futures or async. Personally, I prefer the style of futures better for some things.

For example, to convert your synchronous for loops to asynchronous for loops (in order to work with the asynchronous prompt.get), try something like this:

var forEachAsync = require('forEachAsync');

forEachAsync([0, 1], function (next, _, i) {
    forEachAsync([0, 1], function (next, _, j) {
        console.log("Enter data for "+i+"th row and "+j+"th column");
        prompt.get(['var1', 'var2'], function (error, result) {
            console.log("var1 is "+result.var1);
            console.log("var2 is "+result.var2);
            next();
        });
    }).then(next);
});

In futures, next is the callback to call when you're ready for the next iteration in the loop. We create a nested, asynchronous forEach. The outer forEach continues when the inner forEach finishes, and the inner forEach continues when the prompt callback is called.

The signature for the callback is the same as the signature for Array.prototype.forEach, except it has a callback added as the first parameter.

Edit:

Not related, but consider using printf-style console.log:

console.log("Enter data for %dth row and %jth column", i, j);

This works in node but not in the browser.

Solution courtesy of: tjameson

Discussion

You can understand the basics of Flow Control and implement your own series function as described in this Online book by Mixus. I found it to be great and helpful for Nodejs async beginners. Book: http://book.mixu.net/node/single.html Chapter: 7 Control Flow

Discussion courtesy of: Prince Nishchal

This recipe can be found in it's original form on Stack Over Flow.