Async problem in async.each
-
Suppose we are in a async.waterfall that It was build before. The problem when I print the array at the end of async.each cicle the array is not order like the array passed like parameter (setUsername).Anyone can help me?
setUsername: is a list of uid
set: is hash
start:0
end:-1var array=[]; function(setUsername,next){ async.each(setUsername, function(uid, call) { db.getSortedSetRange(set + uid, start,end, function(err, test) { if (err) { call(err); } array.push({ 'uid': uid, 'test': test }); console.log('UID PUT IN ARRAY '+uid); call(); }); }, function close(err) { if (err) { next(err); } next(null, array); }); }
-
You need to use async.map, which creates the new array based on the old one.
function(setUsername,next){ async.map(setUsername, function(uid, call) { db.getSortedSetRange(set + uid, start,end, function(err, test) { // You need to ignore errors here, and just mark test as null. if (!test) test = null; // The callback will push to the new array. call(null, { 'uid': uid, 'test': test }); }); }, function (err, array) { if (err) { next(err); } // Filter out empty values. array = array.filter(function(el){ return el.test; }); next(null, array); }); }
Copyright © 2024 NodeBB | Contributors