I am trying to call a request inside of a forEach loop, but my code is exiting the loop before the loop is done. Is there a way to ensure the loop finishes before the next code is executed? (I am relatively new to promises)
My code follows this format:
let arr = [1, 2, 3, 4, 5, 6];
arr.forEach(num => {
return request('http://google.com')
.get('/')
.then(() => {
console.log(num);
});
});
console.log('HERE');
This code ^ logs
HERE
1
2
6
4
5
3
(the numbers are in random order, that's not really what matters to me)
but I want it to log
1
2
3
4
5
6
HERE
How do I do this?