javascript - Control flow: Run two asynchronous array maps -
i have 2 functions asynchronous – accept function parameter called when done (callback).
function a(item, cb) { someasyncoperation(function () { cb(item) }) } function b(item, cb) { someasyncoperation(function () { cb(item) }) }
i have array. need run these functions, using array.prototype.map
, on array 2 times. when both maps done, have callback invoked 2 parameters: error , mapped array.
what sort of control flow need achieve this? in async library i'm guessing.
in pseudo-ish code:
var example = [1, 2, 3] async.series([ function () { example.map(a) }, function () { example.map(b) } ], function (error, mappedexample) { })
you can use async library sort of thing, in case, can handle chaining functions together:
var items = ["a", "b", "c", "d" ], foo = function (array, cb) { "use strict"; array = array.map(function (element) { return "foo-" + element; }); cb(array); }, baz = function (array) { "use strict"; array = array.map(function (element) { return "baz-" + element; }); console.log(array); }; foo(items, baz);
results in:
[ 'baz-foo-a', 'baz-foo-b', 'baz-foo-c', 'baz-foo-d' ]
if functions particularly complex or sort of thing often, async can of help; however, there no reason why can't on own. (you need add error handling of course)
Comments
Post a Comment