for api request I go trough some arrays:
'mail', '20.03.2017 05:32', '11.07.2017 03:44', '2', '0', '2', '0', '4', '3', '46' ]
for each array I do 3 nested request But now I have the problem that my index for the requests is sometimes wrong. I think that my loop is faster than the request. How can I take the following way:
- Start for loop index = 1
- make api call 1 with index 1
- wait for api call 1 response
- make api call 2 with index 1
- wait for response of api call 2
- make api call 3 with index 1
wait for response of api call 3
index++
make api call 1 with index 2 ......
for (var i = 1; i <= (csvData.length-1); i++){
var options = { method: 'GET',
url: 'https://api.pipedrive.com/v1/persons/find',
qs:
{ term: csvData[i][0],
start: '0',
search_by_email: '1',
api_token: '' },
headers:
{ 'postman-token': '',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
var user = JSON.parse(body);
console.log("-->User gefunden<--");
console.log("-->User: "+user.data[0].name+"<--");
var options = { method: 'GET',
url: 'https://api.pipedrive.com/v1/deals/find',
qs:
{ term: user.data[0].name,
api_token: '' },
headers:
{ 'postman-token': '',
'cache-control': 'no-cache' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
var deal = JSON.parse(body);
var options = { method: 'PUT',
url: 'https://api.pipedrive.com/v1/deals/'+deal.data[0].id,
qs: { api_token: '' },
headers:
{ 'postman-token': '',
'cache-control': 'no-cache',
'content-type': 'application/json' },
body: { stage_id: csvData[i-1][9]},
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
});
});
});
}
});
-
1Firstly you have a syntax error. Please fix that first.Aron– Aron2017年07月24日 12:32:11 +00:00Commented Jul 24, 2017 at 12:32
4 Answers 4
You need to use : -
async.eachOfSeries(coll, iteratee, callback) as parent loop and
async.eachSeries() as a child loop.
for more details see async library :
Comments
You need to use closures How do JavaScript closures work?. You cannot guarantee values when it comes to async.
1 Comment
Use the series method of the Async library.
async.series(['mail', '20.03.2017 05:32', '11.07.2017 03:44', '2', '0', '2', '0', '4', '3', '46' ].map( elem => {
return done => { // Building an array of functions to pass to Async. They will be executed one after the other, when done() is called
callYourApi( elem, result => done(null,result) ) // 1st argument = error, null == no error
}
},
(err, results) => { // Global callback when all the calls are finished (or when an error occured in the chain)
// results is now an array of all the "result" of every call
})
Comments
Buddy .
firstly the syntax you are doing is wrong.
IF you want 1 request than 2 request than 3 request
you have to do like this
request(options1,function(){
request(options2,function(){
request(options3,function(){
// callback here
});
});
});
and apply async or promises instead of for loop.
check async or promise in node.