Can anyone point me to an example or share a code snippet of how to chain together multiple requests, when the result of the first request must be used to make the second request, etc.?
You can chain the results as they are regular promises:
axios.get(...)
.then((response) => {
return axios.get(...); // using response.data
})
.then((response) => {
console.log('Response', response);
});
In this example, response
comes back as undefined even when the request is successful?
No, it should be defined. Mind the return
statement. You've probably missed it.
ayyyyy 👍
And how would you catch the individual errors in this case?
I have a loop(for) to send axios post,and i have found that each request hasn't send out after each loop,they had send out together after thees loop over
this is my code
`
for (let i = 0;i < differ;i++) {
tem.start = moment(start).add(i, 'days').format('YYYY-MM-DD')
tem.end = moment(tem.start).endOf('day').format('YYYY-MM-DD 23:59:59')
tem.date = tem.start
// if (V.hasSameValue(title, tem.start, tem.end))continue
V.addEventsToCalendar(tem)
}
addEventsToCalendar: (event) => {
axios.post(requestUrl('calendar'), {...event})
.then(res => {
V.eventsRender(V.dataToEvent([res.data]))
successTip()
})
.catch(errorTip)
},
`
@Liaohuanle
For is asynchronous, it doesn't wait end of each operation. Instead of loop you can use simple recursion.
Why don't just try this?
axios.get(...)
.then((response) => {
axios.get(...)
.then((response) => {
console.log('response);
})
})
@alejogamboa Yet another callback hell, what do you think?
var array = [0,1,2];
axios.get('api/' + array[0]).then(response => {
axios.get('api/' + array[1]).then(response => {
axios.get('api/' + array[2]).then(response => {
alert('done');
});
});
});
how do I chain it based on array?
@jericopulvera have a look at using
axios.all([])
for (...) {
arr.push(axios.get("url"));
}
let result = await axios.all(arr);
console.log(res);
Already got the solution here #1413
Most helpful comment
You can chain the results as they are regular promises: