Created
August 31, 2021 14:40
-
-
Save sharjeel619/b990a405271375180541071cff6052ee to your computer and use it in GitHub Desktop.
Async/Await in a Loop | Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sequentially get data from API while waiting for previous call to finish | |
for (let temp of arr) { | |
const res = await fetch('https://jsonplaceholder.typicode.com/users/' + temp).then(res => res.json()).then(res => res); | |
} | |
// sequentially get data from API asynchronously | |
const promises = Array(10).fill(null).map((item, index) => fetch(`https://jsonplaceholder.typicode.com/users/${index + 1}`).then(res => res.json()).then(res => res)); | |
for await (const item of promises) { | |
console.log(await item) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment