javascript - Asynchronous code and loops within Node.js? -
i've done hours of research on asynchronous programming, can't seem grasp 1 single concept within node, wondering if here me.
i've written following code sample return / output simple string concatenation of strings object:
var itemcollection = { item1 : [{ foo : "bar" }, { foo : "bar" }, { foo : "bar" }], item2 : [{ foo : "bar" }, { foo : "bar" }, { foo : "bar" }], item3 : [{ foo : "bar" }, { foo : "bar" }, { foo : "bar" }] } var astring = ""; for(item in itemcollection){ (var = 0; < itemcollection[item].length; i++) { var anitem = itemcollection[item][i]; //somefunctionthatdoesalongiooperation takes item param, plus callback. somefunctionthatdoesalongiooperation(anitem, function(databackfromthisfunction){ // data returned function astring += databackfromthisfunction.datatoappend; }); }; } console.log(astring);
so understand, languages other javascript run somefunctionthatdoesalongiooperation
synchronously , script run in 'blocking mode'. mean value astring
returned / outputted correct value.
however, node runs asynchronously, code can continue run @ anytime , tasks may not complete in order. because of way event loop works in node. think this.
so question comes in. if wanted value astring
returned / outputted correct value in other languages, need loops within code example? or put question in more technical words: correct approach making astring
return expected result, io operations (which take longer amount of time run) aren't completed after script has finished executing when astring
has been returned?
i hope question makes sense, if doesn't, please let me know , make edits appropriate.
thank you
since function apply each item asynchronous, loop processes them must asynchronous (likewise function consumes result of loop must async). check out bob nystrom's "what color function?" more insight on particular point.
there's 2 ways (both using caolan's async
library wrap nasty callback logic):
do 1 async operation 1 @ time, waiting previous finish before next can begin. similar way traditional synchronous loop runs. can
async.reduce
:async.reduce(itemcollection, "", function(memo, item, callback) { somefunctionthatdoesalongiooperation(item, function(databackfromthisfunction) { callback(null, memo + databackfromthisfunction.datatoappend); }); }, function(err, result) { var astring = result; });
of course, there's little point in having async code if don't reap it's benefits , execute many things @ once. can async operations in parallel , reduce @ once in single step afterwards. i've found great if processing each item requires long operation such network i/o, since can kick off , wait many requests @ once. use
async.map
achieve this:async.map(itemcollection, function(item, cb) { somefunctionthatdoesalongiooperation(item, function(databackfromthisfunction) { cb(null, databackfromthisfunction.datatoappend); }); }, function(err, results) { var astring = results.join(''); });
Comments
Post a Comment