Reputation: 2766
I have tried the following code snippet:
var arrVal = [[1,2],[[2,3],[4,5]],3,4];
var async = require('async');
var recurse = function(arr , callback) {
async.eachSeries(arr, function(eachVal, callback) {
if(typeof eachVal == "object") {
recurse(eachVal);
}
else {
console.log(eachVal);
callback(null);
}
}, callback);};
recurse(arrVal);
Expected to print all the numbers present in arrVal
array, but I am getting only 1,2 (Numbers of 0th index of the array).
What I am doing wrong? Can someone guide with the better way to achieve what I want to get?
NOTE: I was hoping to do this asynchronously
Upvotes: 1
Views: 133
Reputation: 787
This is working fine in your code there is no callback in if condition. if you want an asynchronous way you can use async.each instead of async.eachSeries async.each
The difference with async.eachSeries is that each iteration will wait for the async operation to complete before starting the next one.
var arrVal = [[1,2],[[2,3],[4,5]],3,4];
var async = require('async');
var recurse = function(arr , callback) {
async.eachSeries(arr, function(eachVal, each_cb) {
if(typeof eachVal === "object") {
recurse(eachVal);
each_cb()
}
else {
console.log(eachVal);
each_cb(null);
}
}, callback);};
recurse(arrVal);
Upvotes: 2