I have the following code:
var Promise = require('bluebird');
var reqP = Promise.promisifyAll(require('request-promise'));
var requestsArray = [];
function getRequests(){
const req1 = {
method: 'GET',
uri: 'url'
}
const req2 = {
method: 'GET',
uri: 'url'
}
const req3 = {
method: 'GET',
uri: 'url'
}
const req4 = {
method: 'GET',
uri: 'url'
}
const req5 = {
method: 'GET',
uri: 'url'
}
const req6 = {
method: 'GET',
uri: 'url'
}
const req7 = {
method: 'GET',
uri: 'url'
}
Promise.all([reqP(req1), reqP(req2), reqP(req3),
reqP(req4), reqP(req5), reqP(req6),
reqP(req7)])
.then(function (results) {
for(re in results){
switch(re){
case '0':
requestsArray.push({'req1':{'value':JSON.parse(results[re])}})
break;
case '1':
requestsArray.push({'req2':{'value':JSON.parse(results[re])}})
break;
case '2':
requestsArray.push({'req3':{'value':JSON.parse(results[re])}})
break;
case '3':
requestsArray.push({'req4':{'value':JSON.parse(results[re])}})
break;
case '4':
requestsArray.push({'req5':{'value':JSON.parse(results[re])}})
break;
case '5':
requestsArray.push({'req6':{'value':JSON.parse(results[re])}})
break;
case '6':
requestsArray.push({'req7':{'value':JSON.parse(results[re])}})
break;
}
}
return requestsArray;
})
.catch(function (err) {
console.log('Error: ', err);
})
}
module.exports = {getExampleFile};
The return requests Array
is being returned empty (or undefined) because if runs without waiting for the for loop to finish.
How can I make it wait for the for loop to finish, as in this case it's already nested in a .then()?
I have been going through a lot of options, starting with Promises, using another function that will be called upon the 6th iteration and going over async await.
Been playing around with it, but still not entirely sure how to implement each and every one of the suggested solutions in that case though.
Thank you.