javascript - Node - Wait request response within cycle For -



javascript - Node - Wait request response within cycle For -

really, i've tried ways, fail find solution.

as no english, , i'm using google translator, explain same thing several times in row.

1º seek loop wait until finished within

2º seek know city doing ok on external web request. , many cities, need break cycle 1 time check first city ok.

3º need go json , execute action while running , wait till finishes follow. out of cycle @ time.

i've been testing several codes, nowadays here improve understanding. --------------------------------------------------------------------- var city = [ {'city':'madrid'}, {'city':'barcelona'}, {'city':'valencia'}, {'city':'malaga'} ]; var vbreak = 0; (i in city){ request('example.there-city.com/' + city[i].city ,function (error, response, read) { if (read == 'ok') { vbreak = 1} }); if (vbreak == 1){ break;} } var city = [ {'city':'madrid'}, {'city':'barcelona'}, {'city':'valencia'}, {'city':'malaga'} ]; var vbreak = 0; (i in city){ (function(i) { request('example.there-city.com/' + city[i].ciudad ,function (error, response, read) { if (read == 'ok') { vbreak = 1} }); })(i); if (vbreak == 1){ break;} }

thank you

use async.eachseries (as jgillich said) perform each request in order this:

async.eachseries(city, function (item, cb) { if (vbreak !== 1) { request('example.there-city.com/' + item.city ,function (error, response, read) { if (read == 'ok') { vbreak = 1} cb(); }); } else { cb() // skip } }, function () { // we're finished })

you'll need add together error handling probably.

javascript json node.js for-loop asynchronous

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -