【发布时间】:2020-08-28 13:02:48
【问题描述】:
如何在 catch 时终止函数执行?我认为 return 应该放在我放的地方(.catch( error => {console.log(error);} return)),但这不起作用。告诉我怎么做才对?
getCustomers: function () {
let url = '/crm/customer/';
axios.get(url).then((response) => {
this.customers = response.data.results;
if (this.customers.length === 100) {
for (let i = 2; i < 100; i++) {
axios.get('/crm/customer/?page=' + i).then((response) => {
this.c = response.data.results;
for (let item of this.c.values()) {
this.customers.push(item);
}
}).catch( global_waiting_stop());
}
}
}).catch( error => { console.log(error); })
.finally(() => (global_waiting_stop()));
},
【问题讨论】:
-
如果我正确地回答了你的问题,如果第二个 axios 失败,你想要退出吗?然后将它们包装在 Promise.all 中并链接它
-
@AniketJha 执行我,但是怎么做呢?我是初学者
-
@AniketJha - Promise.all 仍将同时运行它们。他需要将其转换为异步函数并等待每个调用......但是连续 97 个 ajax 调用......这将需要很长时间。当然,API 有某种分页机制,他们并没有完全像这样猜测。
-
@Iwrestledabearonce。是否有可能提前知道可以获取多少页?我做这一切只是为了从所有页面获取数据,我把 100 留有边距,因为会添加新的
-
@Iwrestledabearonce。首先,您的用户名很棒。是的,Promise.all 会同时进行许多网络调用,是的,在 API 级别应该有一些分页机制来限制调用。我会触发页面 api 调用等待数据然后发出新调用。 Ekzo 是否真的需要在这一点上拥有页面数据?
标签: javascript vue.js axios