javascript - node.js synchronous requests for use in caching -
i have issue need resolve in code has caching api results. have async.map so:
for(var user of allusers) { var requestlist = fill(cache, locations, apiurl, cachemap); async.map(requestlist, function(obj, callback) { // ... }, function(err, results) { // put results cache }); }
the function fill
looks within cache see if location in locations
exists, , not creates request url api run.
i'm realizing cache won't use @ approach, because code dispatch async.map , start on next loop iterations fill
, meaning cache won't synchronized @ each iteration of user.
how go ensuring each iteration of user has updated version of cache last user? need make smart use of limited api calls, if there duplicated requests want request once, pull result cache in later requests.
my throught right to synchronized request instead of async.map, know goes against design of node.js.
for(var user of allusers) { var requestlist = fill(cache, locations, apiurl, cachemap); // sync map instead requestlist.map(function(obj) { var res = sync-request(obj) // put request result cache }); // cont... }
you iterate on allusers
using async.eachseries. step through in order , keep asynchronous.
async.eachseries(allusers, (user, done) => { const requestlist = fill(cache, locations, apiurl, cachemap); async.map(requestlist, (obj, callback) => { // .. callback(null, requestresult); }, (err, results) => { // put results cache done(null, results); }); }, cb);
Comments
Post a Comment