I am learning JavaScript and just went over Promises. I tried to write a loop that looked similar to this:
for (let i of someArray) {
if (condition) {
await returnsPromise(i)
}
}
This code does not work, and I was wondering if there is any other syntax that I would have to be aware of.
2
Answers
To use await inside a loop, you need to define an async function and then call it within that function.
the
await
code must still require theasync function
as a caller. or you can usedeno.js
then your code will work fine. no need forasync function
anymore.try my code it works fine