node.js - NodeJs : Using async for request url - Post method sending response before execution of all urls -


i have post method, request input list of newlink objects (newlink attributes linkurl , status) using async.map iterating on urls check if links active or not.

newlinks contains links {www.google.com,www.nourl.com,www.xyz.com} expecting after request processed , setting corresponding status true or false, want send using res.send(newlinks)

but console giving below results: "www.google.com up", calling res.send(), executing "www.nourl.com up" , "www.xyz.com up"

so here, after first url request , code below executing function outside async loop. thought async allow next piece of code execute after urls validated.

app.post('/myposturl', function(req , res){ var request = require('request'); let linkdetails= req.body.linkdetails; var = 0;  async.map(linkdetails, function(newlink, callback) { var url = "url"; var url = newlink.linkurl;         var proxiedrequest = request.defaults({'proxy': url});         proxiedrequest(url , function (error, response, body) {            if(error){             console.log('err: '+ error);           }         if (!error) {         if(response.statuscode == 200 || response.statuscode == 201 ||           response.statuscode == 202){           console.log(url + ' up!!');           newlink.isactive = true;         }          if(response.statuscode == 301 || response.statuscode == 302){           console.log(url + ' redirecting us!!');           return false;         }         }         });         callback();         }  , function(err, linkdetails) {        res.send(linkdetails);         });       //tried res.send here well.         });     } 

the callback of async.map should invoke inside proxiedrequest. code doing now: invoke callback before proxiedrequest finished. return false; not work in asynchronous function. should return new status callback(null, newlink). after request processed, newlinkdetails array of newlink.

note, since function applies iteratee each item in parallel, there no guarantee iteratee functions complete in order.

if need keep order, user mapseries insted.

please read doc of async.map more. hope helps.

app.post('/myposturl', function(req , res){   //other codes   async.map(linkdetails, function(newlink, callback) {     //other codes     proxiedrequest(url , function (error, response, body) {         if(error){             console.log('err: '+ error);             callback(error);             //^ ^  ^  ^  ^             // validation failed, return here         }         else {             //some validation & set newlink.isactive              callback(null, newlink);             // ^   ^    ^   ^    ^             //return newlink status invoking callback         }     });   }, function(err, newlinkdetails) {       //err = if validation failed       // request processed,newlinkdetails array of newlink's       res.send(newlinkdetails);   }); }); 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -