Not all loop work the same in JavaScript. If you want an asynchronous loop you cannot use forEach
because it will execute each iteration independently, however if you use a for of
it will work as expected: it will await until the previous iteration has finished.
async () => {
for (const e of elements)
await getCloudData(e)
}
Be aware that in the previous code you will have to wait for each promise to resolve sequentially, which may not be as efficient if all your calls are unrelated. In that scenario, I would recommend you to use Promise.all()
that would create parallel connections and will be just as late as the latest response.
async () => {
let promises = []
for (const e of elements)
promises.push(getCloudData(e))
await Promise.all(promises)
}
Keep in mind that every browser has a specific limit to open parallel connections.
Get Permutations
/in JavaScript/by GénesisRotate Markers in Google Maps
/in JavaScript/by GénesisStill as of today (post’s date) you cannot rotate markers in Google Maps, you only can resize them, however if you are using SVGs (what I always recommend) you can do any transformation you want 🙂
Tracking changes in the DOM
/in HTML5, JavaScript/by GénesisCSS 3D Layers Effect
/in CSS3, HTML5/by GénesisAsynchronous iteration
/in JavaScript/by GénesisNot all loop work the same in JavaScript. If you want an asynchronous loop you cannot use
forEach
because it will execute each iteration independently, however if you use afor of
it will work as expected: it will await until the previous iteration has finished.Be aware that in the previous code you will have to wait for each promise to resolve sequentially, which may not be as efficient if all your calls are unrelated. In that scenario, I would recommend you to use
Promise.all()
that would create parallel connections and will be just as late as the latest response.Keep in mind that every browser has a specific limit to open parallel connections.