Sometimes, we want to create a JavaScript ES6 promise for loop.
In this article, we’ll look at how to create a JavaScript ES6 promise for loop.
How to create a JavaScript ES6 promise for loop?
To create a JavaScript ES6 promise for loop, we use a for-of loop.
For instance, we write
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
(async function loop() {
for (const d of delays) {
await delay(d);
}
})();
to create a loop
function that runs a for-of loop to loop through the delays
array.
In the loop, we call delay
with d
in delays
which returns a promise.
And we wait for delay
to finish running with await
in each iteration.
Conclusion
To create a JavaScript ES6 promise for loop, we use a for-of loop.