Skip to content

Instantly share code, notes, and snippets.

@neuralline
Created December 21, 2020 03:14
Show Gist options
  • Save neuralline/0ad4bc39d56b2ece2b1b0259a9904432 to your computer and use it in GitHub Desktop.
Save neuralline/0ad4bc39d56b2ece2b1b0259a9904432 to your computer and use it in GitHub Desktop.
Promise all fetch array of urls using async/await
const axios = require('axios')
//using axios because node does not support fetch out of the box
const requestMultipleUrls = async urls => {
try {
const response = await Promise.all(
urls.map(async url => {
const res = await await axios(url)
return res.data
})
)
//on success return data
return response
} catch (err) {
console.log('error while connecting to server: ', err)
//in case of error reject promise
return undefined
}
}
module.exports = requestMultipleUrls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment