Answer the question
In order to leave comments, you need to log in
How to make multiple parallel queries and return 1 result?
It is necessary to execute axios several parallel requests and return the overall result. There is a code, but it returns the result from only one of the requests (after each refresh, randomly, then from one, then from the other)
app.get('/pets', (req, res) => {
async.parallel({
cat: function (callback) {
axios('http://localhost:3001/cat')
.then((response) => {
callback(response.data)
})
.catch((err) => {
callback(err)
})
},
dog: function (callback) {
axios('http://localhost:3002/dog')
.then((response) => {
callback(response.data)
})
.catch((err) => {
callback(err)
})
}
},
function(results, error) {
res.json({
results: results,
error: error
})
})
})
Answer the question
In order to leave comments, you need to log in
const cat = axios('http://localhost:3001/cat').then(res => res.data);
const dog = axios('http://localhost:3002/dog').then(res => res.data);
Promise.all([cat, dog])
.then(pets => console.log(pets))
.catch(error => console.log(error));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question