Created
January 25, 2022 13:17
-
-
Save abcdeepakr/449876eac9b08a4704aa465114a2ad13 to your computer and use it in GitHub Desktop.
promises to make async api calls
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
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 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the corrections, i wrote it while it was on top of my mind. appreciate it