【发布时间】:2019-05-13 05:15:56
【问题描述】:
我有 6 个异步请求。如果其中一个出错,返回 404,其他请求也不起作用。我使用async.parallel 提出这些请求。当其中一个请求失败时,我正在尝试提出其他请求。但我做不到。
这是我的代码:
async.parallel({
request1: async (callback) => {
const [err, result] = await to(this.$store.dispatch('myAction1', {
id: this.$route.params.id,
}));
callback(err, result);
},
request2: async (callback) => {
const [err, result] = await to(this.$store.dispatch('myAction2', {
params: {
id: this.$route.params.id,
page: this.page,
size: this.size,
},
}));
callback(err, result);
},
request3: async (callback) => {
const [err, result] = await to(this.$store.dispatch('myAction3', {
data: {
filters: this.inputs.filters,
projections: this.inputs.projections,
showTotalCount: this.inputs.showTotalCount,
},
params: {
page: this.page,
size: this.size,
},
}));
callback(err, result);
},
request4: async (callback) => {
const [err, result] = await to(this.$store.dispatch('myAction4'));
callback(err, result);
},
request5: async (callback) => {
const [err, result] = await to(this.$store.dispatch('myAction5', {
id: this.$route.params.id,
}));
callback(err, result);
},
request6: async (callback) => {
const [err, result] = await to(this.$store.dispatch('myAction6', {
params: {
id: this.$route.params.id,
},
}));
callback(err, result);
},
}, (err, results) => {
if (err) {
// Show error message when one of them fails
}
// doing something when all requests success and hide the page loader.
this.hidePageLoader();
});
如果这些请求中的一个返回 404,则此代码始终显示页面加载器,我想将失败的请求作为 null 传递给我的 results 对象或返回其他结果,而 results 对象中的请求没有失败。我怎样才能使它正确
【问题讨论】:
标签: javascript ajax asynchronous vue.js request