Skip to content

Instantly share code, notes, and snippets.

@sharjeel619
Created August 31, 2021 14:40
Show Gist options
  • Save sharjeel619/b990a405271375180541071cff6052ee to your computer and use it in GitHub Desktop.
Save sharjeel619/b990a405271375180541071cff6052ee to your computer and use it in GitHub Desktop.
Async/Await in a Loop | Javascript
// 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