Created
December 21, 2020 03:14
-
-
Save neuralline/0ad4bc39d56b2ece2b1b0259a9904432 to your computer and use it in GitHub Desktop.
Promise all fetch array of urls using async/await
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 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