Reputation: 827
I have this arrays with 8 items each
var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"];
I'm trying to do a post request with each index of the arrays:
function test2(){
var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"];
var count = 8;
for (var i=0; i<count; i++){
var pullRequests_id = array_pullrequest_id[i];
var createdBy = array_uniqueName[i];
console.log("first index: " + i);
console.log("first console log pullRequest ID: " + pullRequests_id);
console.log("first console log Created by: " + createdBy);
var options = {
'method': 'GET',
'url': 'https://HIDEN_URL/pullRequests/'+ pullRequests_id+'/workitems',
'headers': {
'Authorization': 'Basic HIDEN_AUTH',
'Cookie': 'HIDEN_COOKIE'
}
}
request(options, function (error, response) {
console.log("second index: " + i);
console.log("second console log pullRequest ID: " + pullRequests_id);
console.log("second console log Created by: " + createdBy);
});
}
}
NoW this is the console output:
first index: 0
first console log pullRequest: 335
first console log Created by: [email protected]
first index: 1
first console log pullRequest: 328
first console log Created by: [email protected]
first index: 2
first console log pullRequest: 326
first console log Created by: [email protected]
first index: 3
first console log pullRequest ID: 323
first console log Created by: [email protected]
first index: 4
first console log pullRequest ID: 322
first console log Created by: [email protected]
first index: 5
first console log pullRequest ID: 314
first console log Created by: [email protected]
first index: 6
first console log pullRequest ID: 295
first console log Created by: [email protected]
first index: 7
first console log pullRequest ID: 291
first console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
second index: 8
second console log pullRequest ID: 291
second console log Created by: [email protected]
Now as you can see the first console log items are print correctly following the count variable index, but inside the for loop request function (second console log) its only printing the last array item, even if its inside the loop, it just take the last, it just doesn't make sense to me...
Upvotes: 1
Views: 138
Reputation: 737
assuming you have installed axios, you can use async/await like below
notice the async
keyword in front of the function test2()
now test2
is an async function
const axios = require('axios');
async function test2(){
var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"];
var count = 8;
for (var i=0; i<count; i++){
var pullRequests_id = array_pullrequest_id[i];
var createdBy = array_uniqueName[i];
console.log("first index: " + i);
console.log("first console log pullRequest ID: " + pullRequests_id);
console.log("first console log Created by: " + createdBy);
var options = {
'method': 'GET',
'url': 'https://HIDEN_URL/pullRequests/'+ pullRequests_id+'/workitems',
'headers': {
'Authorization': 'Basic HIDEN_AUTH',
'Cookie': 'HIDEN_COOKIE'
}
}
await axios(options)
.then(() => {
console.log("second index: " + i);
console.log("second console log pullRequest ID: " + pullRequests_id);
console.log("second console log Created by: " + createdBy);
})
}
}
Upvotes: 1
Reputation: 13
You can also use async for loop "if your node version supports it" , as said above the problem with your code is that the request is asynchronous by nature.
Upvotes: 0
Reputation: 71
Making service request is blocking in nature so we should use either callbacks or promises to get output as you expect. try to use asyncForEach with this you can able to achieve above output.
async function test2() {
var array_pullrequest_id = ["335", "328", "326", "323", "322", "314", "295", "291"];
var array_uniqueName = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"];
var count = 8;
await asyncForEach(array_pullrequest_id, async(pullrequest, index) => {
var pullRequests_id = array_pullrequest_id[index];
var createdBy = array_uniqueName[index];
console.log("first index: " + index);
console.log("first console log pullRequest ID: " + pullRequests_id);
console.log("first console log Created by: " + createdBy);
var options = {
'method': 'GET',
'url': 'https://HIDEN_URL/pullRequests/' + pullRequests_id + '/workitems',
'headers': {
'Authorization': 'Basic HIDEN_AUTH',
'Cookie': 'HIDEN_COOKIE'
}
}
request(options, function(error, response) {
console.log(error);
console.log("second index: " + index);
console.log("second console log pullRequest ID: " + pullRequests_id);
console.log("second console log Created by: " + createdBy);
});
});
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
Upvotes: 0
Reputation: 4519
The reason why the second console log prints only the last item in the array because it's inside request
which is an asynchronous
function, which means while request
function is running in the background to return a promise the rest of your code outside the request function is running as normal and so the loop keeps iterating until the last item in the array
Upvotes: 0