javascript - How to convert a function to an async.each function -


i've small problem function in class of nodejs/express server.

i have following function :

@resolveservers = (url, servers, resolved) ->   result = []   treatserver(url, server, (serverobject) ->     result.push serverobject     resolved result if result.length servers.length   ) server in servers 

and since treat server function can take wild, , hinted on question of mine, considering using async each this.

so i'm here :

@resolveservers = (url, servers, resolved) ->   result = []   async.each(servers, treatserver(url, server, (serverobject) ->     result.push serverobject         resolved result if result.length servers.length   ), (err) ->     if err        console.log next err   ) 

and following error :

referenceerror: server not defined 

the treatserver function needs url , server servers array. don't know how server instance each 1 of them, , pass treatserver function.

any idea on i'm doing wrong ?

thanks already.

update: i'm disappointed have no gain @ using async. function used takes approximatively 6500ms , async 1 same. guess each function coffeescript job well.

update: after further tests, i'm finding out function slower async. wierd.

i think want async.map instead: gathers results of operations , passes them array final callback. maintains order of input array, async.each doesn't (but might not issue).

@resolveservers = (url, servers, resolved) ->   async.map(servers, (server, callback) ->     treatserver(url, server, (serverobject) ->       callback null, serverobject     )   , (err, result) ->     if err        console.log next err     resolved err, result // see text   ) 

since cf isn't great, js version too:

function resolveservers(url, servers, resolved) {   async.map(servers, function(server, callback) {     treatserver(url, server, function(serverobject) {       callback(null, serverobject);     });   }, function(err, result) {     if (err)       console.log(err);     resolved(err, result);   }); } 

i suggest following common node idiom asynchronous callbacks, first argument error object (or null no errors occurred), , second argument result value. way, can propagate errors way calling chain.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -