JavaScript承诺拒绝



我在我的服务中返回Promise;

@Action
[Actions.LOGIN](credentials) {
return new Promise<void>((resolve, reject) => {
ApiService.post("auth/login", credentials)
.then(({ data }) => {
this.context.commit(Mutations.SET_AUTH, data);
resolve();
})
.catch(({ response }) => {
this.context.commit(Mutations.SET_ERROR, response.data.message);
reject();
});
});
}

在我的sign.vue文件中,我试图捕获错误;

store
.dispatch(Actions.LOGIN, values)
.then(() => {
Swal.fire({
text: "All is cool! Now you submit this form",
icon: "success",
buttonsStyling: false,
confirmButtonText: "Ok, got it!",
customClass: {
confirmButton: "btn fw-bold btn-light-primary",
},
}).then(function () {
// Go to page after successfully login
router.push({ name: "dashboard" });
});
})
.catch(() => {
Swal.fire({
text: store.getters.getErrors[0],
icon: "error",
buttonsStyling: false,
confirmButtonText: "Try again!",
customClass: {
confirmButton: "btn fw-bold btn-light-danger",
},
});
});

在服务中返回错误时,捕获块正在捕获。但在vue文件中,catch block不捕获,它会阻塞。为什么会这样呢?拒绝命令是错误的吗?

如果期望在catch块中有延续,则必须返回承诺:

.then(() => {
return Swal.fire({
/* ... */
});
})

最新更新