Skip to content

Instantly share code, notes, and snippets.

@abcdeepakr
Created January 25, 2022 13:17
Show Gist options
  • Save abcdeepakr/449876eac9b08a4704aa465114a2ad13 to your computer and use it in GitHub Desktop.
Save abcdeepakr/449876eac9b08a4704aa465114a2ad13 to your computer and use it in GitHub Desktop.
promises to make async api calls
const fetchData = async () =>{
const ids = [1,2,3,4,5]
let promises = await ids.map(id =>{
await axios.get(`https://jsonplaceholder.typicode.com/posts/${id}`)
.then(res => res.data)
.catch(err => console.log(err)
})
let posts = await Promise.all(promises).then(res => res)
console.log(posts) // fetches all posts and returns
}
@abcdeepakr
Copy link
Author

Thanks for the corrections, i wrote it while it was on top of my mind. appreciate it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment