我有一个有10行(可能不同)的数据的表单,在提交时,我需要进行10(可能不同)的api调用,每行一个。我使用axios进行api调用。如何才能以最佳且高效的方式在一次单击中调用多个api?
发布于 2020-02-12 15:30:36
Axios支持Axios api,因此您可以使用Promise.all
一次处理所有10个请求。下面是一个小示例:
const requests = [
{ url: "https://some.url", body: { some: "body" } },
{ url: "https://some.other.url", body: { some: "other body" } },
// As many as you like
];
const promises = requests.map(request => axios.post(request.url, request.body));
const result = Promise.all(promises).catch(error => console.log(`Someting went wrong: ${error}`);
发布于 2020-02-12 17:08:52
你也可以使用Bluebird。
import Bluebird from 'bluebird';
deleteRequests = (requests) => {
let promiseCollection = [];
try {
requests.map((request, index) => {
promiseCollection.push(axios.delete(request.API + request.ids));
});
return Bluebird.all(promiseCollection);
}
catch (error) {
}
}
https://stackoverflow.com/questions/60182843
复制相似问题