使用Kris Kowal的Q.如果在链式承诺的整个生命周期中发生任何错误,我应该如何捕获? [英] Using Kris Kowal's Q. How should I catch if any errors have been thrown throughout the life of a chained promise?

查看:83
本文介绍了使用Kris Kowal的Q.如果在链式承诺的整个生命周期中发生任何错误,我应该如何捕获?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是承诺的新手,我刚刚开始使用 Kris Kowal的Q 今天(我以前读过它并使用它与Angular.js一点),我正在使用jQuery进行Ajax调用并将其转换为Q版承诺。我试图找到一种很好的方法来最终获得某种捕获,告诉我这个承诺是否在某一时刻被拒绝,所以我可以告诉用户一路上出了什么问题。

I'm new to promises, and I just started using Kris Kowal's Q today (I've read about it before and used it a little with Angular.js), and I was doing an Ajax call with jQuery and turning it into a Q promise. I was trying to find a good way to have some sort of catch at the end that would tell me whether this promise was rejected at one point or not so I could tell the user something went wrong along the way.

我的想法是,无论我的承诺有多长,我都希望将这个想法扩展到工作状态,最后我想要检查它是否在承诺期间的任何时候失败了。这就是我最终编写的内容,但我不想在我的每个失败/捕获中抛出一个新错误,以便在promise链中传播错误。我想要一些不太奇怪的东西。

The idea is I want to extend the idea to work no matter how long my promise and at the end I want a check to see if it failed at anytime during the promise. This is what I ended up writing, but I wouldn't want to throw a new error in each of my fails/catches like this in order to propogate the error down the promise chain. I want something less weird.

function deleteEvent(id){
    var url = "sampleURL/deleteEvent?id=" + id;
    return Q($.ajax({
        type: 'POST',
        url: url,
        dataType: 'json'
    })).then(function (data) { // success
        // on success
        UpdateDOMforEventsChanged();
    }).fail(function (xhr) { // failure
        console.log(xhr);
        throw new Error('Problem deleting Event');
    });
}

以下是我在其他地方延长此承诺的方式

And here's how I extended this promise elsewhere

deleteEvent(event._id).then(function () { // AJAX call was a success delete the things
    $('#calendar').fullCalendar('removeEvents', event._id);
}).done(null, function () { // It failed somewhere alert the user.
    alert("There was an error with deleting the event. Check your network connection and try again.")
});

所以基本上我已经注意到,如果我拿出错误,我会投入失败我的deleteEvent部分承诺执行以下然后,它在扩展承诺之后执行,即它执行从fullCalendar删除事件客户端。 如果失败我不希望它这样做,我该如何解决这个问题?也有这样做的原因吗?我猜它可能是,因为你可以以某种方式解决错误或设置默认值并继续承诺链......是吗?

So basically I've noticed that if I take out the error that I'm throwing in the fail section of my deleteEvent promise that it executes the following then that comes after it in the extended promise i.e. it executes deleting the events client side from fullCalendar. I don't want it to do this if it fails, how can I fix this? Also is there a reason why it does this? I'm guessing it might be, because you can resolve the error somehow or set a default and continue the promise chain...is that right?

我还注意到,如果我没有抛出错误,它会跳过done方法的失败回调函数。我知道这是因为q文档说明了以下关于它的 .done()方法,

I also notice if I don't throw the error it skips over the fail callback function of the done method. I know this is because the q documentation says the following about it's .done() method,


如果有未处理的拒绝,要么是因为承诺是
被拒绝而没有提供onRejected回调,要么因为
onFulfilled或onRejected引发错误或返回被拒绝
承诺,由此产生的拒绝原因在事件循环的
未来回合中作为例外抛出。

If there is an unhandled rejection, either because promise is rejected and no onRejected callback was provided, or because onFulfilled or onRejected threw an error or returned a rejected promise, the resulting rejection reason is thrown as an exception in a future turn of the event loop.

所以我假设我不能使用 .done()来捕获所有拒绝,包括已处理的拒绝,(我在猜测处理ajax错误,因为我之后有捕获/失败),但有什么替代方案?

So I'm assuming I can't use .done() to catch all rejections including handled ones, (and I'm guessing in the ajax error is handled since I have the catch/fail after it), but what alternative is there?

推荐答案

如果你没有投入失败,这意味着你从错误中恢复成功(参见:拒绝成功 [使用游标]介意失败 catch的别名),结果承诺最终成功。它不能是其他方式

If you don't throw in fail it means you recover from error to success (see: Rejection into success [use cursors] mind fail is alias for catch), so result promise ends as successful. It can't be other way

而不是使用失败看看Q是否提供了提供对结果和返回自我承诺(类似 搁置 )实现我保持不变。

Instead of using fail see if Q provides function which provides access to result and returns self promise (something like aside in implementation I mantain).

此外,您的流量可以更简单,只需然后,请参阅:

Also your flow can be simpler with less then's, see:

function deleteEvent(id) {
    return Q($.ajax({
        ....
    })).then(function (data) { // success
       // on success
       UpdateDOMforEventsChanged();
    }); // No need for error handler
};

deleteEvent(event._id).done(function () { // AJAX call was a success delete the things
    $('#calendar').fullCalendar('removeEvents', event._id);
}, , function (e) { // It failed somewhere alert the user.
    console.error(e.stack || e.message);
    alert("There was an error with deleting the event. Check your network connection and try again.")
});

这篇关于使用Kris Kowal的Q.如果在链式承诺的整个生命周期中发生任何错误,我应该如何捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆