Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何避免Promise中的重复捕获功能_Javascript_Asynchronous_Promise_Async Await - Fatal编程技术网

Javascript 如何避免Promise中的重复捕获功能

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",

最近,我在VueJS中使用了许多Promises。我必须在我的项目中多次重复
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
});
这可能与此问题重复: