Javascript 如何避免Promise中的重复捕获功能
最近,我在VueJS中使用了许多Promises。我必须在我的项目中多次重复Javascript 如何避免Promise中的重复捕获功能,javascript,asynchronous,promise,async-await,Javascript,Asynchronous,Promise,Async Await,最近,我在VueJS中使用了许多Promises。我必须在我的项目中多次重复catch功能。如何处理代码中单个位置的错误 saveLocation(_, data) { const self = this._vm.$nuxt; self.$api.Store.Add_Location(data) .then(() => { self.$notification("success",
catch
功能。如何处理代码中单个位置的错误
saveLocation(_, data) {
const self = this._vm.$nuxt;
self.$api.Store.Add_Location(data)
.then(() => {
self.$notification("success", self.$t("message.actionSuccess"));
self.$router.push(PUSH_BACK_URL);
})
.catch(self.$commitError);
},z
updateLocation(_, { payload, push }) {
const self = this._vm.$nuxt;
self.$api.Store.Update_Location(payload)
.then(() => {
self.$notification("success", self.$t("message.actionSuccess"));
if (push) self.$router.push(PUSH_BACK_URL);
})
.catch(self.$commitError);
},
destroyLocation(_, { payload, push }) {
const self = this._vm.$nuxt;
self.$api.Store.Delete_Location(payload)
.then(() => {
self.$notification("success", self.$t("location.deleteSuccessMessage"));
if (push) self.$router.push(PUSH_BACK_URL);
})
.catch(self.$commitError);
},
您可以跳过为每个承诺编写catch块&捕获此全局承诺捕获处理程序中的错误
window.addEventListener('unhandledrejection', function(event) {
alert(event.promise); // [object Promise] - the promise that generated the error
alert(event.reason); // Error: Whoops! - the unhandled error object
});
这可能与此问题重复: