axios.all([
axios.get('https://jsonplaceholder.typicode.com/posts/1'),
axios.get('https://jsonplaceholder.typicode.com/posts/2')
])
.then(axios.spread((post1, post2) => {
console.log('Post 1:', post1.data);
console.log('Post 2:', post2.data);
}))
.catch(error => {
console.error('Error fetching data:', error);
});
- We use
axios.all()
to send multiple requests. - The
axios.spread()
function is used to handle the responses separately.