You have a for loop that runs 5 times. Each time the loop runs, it makes an API call. But the next iteration starts only after the API call has finished.
The problem was designed so that I was forced to think we needed to pause the iteration.
But how? The continue keyword skips the iteration, and the break keyword exits the loop. How do we pause?
Solution: We can use async and await to solve this problem.
test();
async function test() {
for (var i = 0; i < 5; i++) {
console.log(await getData());
}
}
async function getData(num) {
const promise = new Promise((res, rej) => {
setTimeout(() => {
res('succesful...');
}, 1000);
});
return promise;
}