Created
February 13, 2023 05:10
-
-
Save valiantboymaksud/9674cc5f6d7d17cff378b2eccab07690 to your computer and use it in GitHub Desktop.
simultaneous HTTP requests with axios
This file contains hidden or 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
Here’s a simple example of how to use axios.all to make simultaneous HTTP requests: | |
// execute simultaneous requests | |
axios.all([ | |
axios.get('https://api.github.com/users/mapbox'), | |
axios.get('https://api.github.com/users/phantomjs') | |
]) | |
.then(responseArr => { | |
//this will be executed only when all requests are complete | |
console.log('Date created: ', responseArr[0].data.created_at); | |
console.log('Date created: ', responseArr[1].data.created_at); | |
}); | |
// logs: | |
// => Date created: 2011-02-04T19:02:13Z | |
// => Date created: 2017-04-03T17:25:46Z | |
ANOTHER AXIOS ALL REQUEST | |
axios.all([ | |
axios.get('https://api.github.com/users/mapbox'), | |
axios.get('https://api.github.com/users/phantomjs') | |
]) | |
.then(axios.spread((user1, user2) => { | |
console.log('Date created: ', user1.data.created_at); | |
console.log('Date created: ', user2.data.created_at); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment