函数返回一个Promise,检查错误 [英] Function Returns a Promise, Check Errors

查看:150
本文介绍了函数返回一个Promise,检查错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数 doSomething(),它使用Q框架返回一个promise链。内容类似于:

I have a function doSomething() that returns a promise chain, utilizing the Q framework. The contents are similar to something like:

loadDataSet : function (params) {

    return Q.fcall(function() {
        //Do Something
    })
    .then(function(){
       //Do Something Else
       throw New Error('unexpected error');
    });
}

调用代码类似于:

var promise = loadDataSet(args);

我想弄清楚是否抛出了这个错误。注意,在 loadDataSet 函数实现中,我没有使用 .done()函数。

I want to figure out whether that error was thrown. Notice, in the loadDataSet function implementation, I did not utilize the .done() function.

到目前为止,我的代码看起来像这样并且在捕获错误和处理错误方面都没有成功(这里,代码被修改,稍微从上面修改):

So far, I have code that looks like this and have been unsuccessful in capturing the error and handling it appropriately (here, the code is modified, slightly from above):

try {
    loadDataSet(args)
    .catch(function(error) {
       return error
    })
    .done();
}....

目标是处理来自try-catch的错误块。我错过了什么?

The goal is to handle the error from a try-catch block. What am I missing?

推荐答案

嗯,这将是一个无赖。

虽然许多承诺库允许你这样做并且会报告未经处理的拒绝 - 在Q中你没有方法自动检测这些故障。

While a lot of promise libraries let you do this and will report unhandled rejections for you - in Q you have no methods to automatically detect these failures.

您必须使用 .done 或更改承诺图书馆。哎呀,即使是原生承诺也可以在几天内完成这项工作

在Q中,唯一可行的选择是使用 .done ,与不同,那么完成并不是安全的,你可以从那里抛出异常并且它们不会被压制 - 这需要你记住总是用完成来终止链条但是它可以工作:

In Q your only realistic option is to use .done, unlike then done is not throw safe and you can throw exceptions from there and they won't be suppressed - this requires you to remember to always terminate chains with done but it works:

myObj.loadDataSet(handleSuccess, handleError).done(); // will throw on rejection

个人在Q修复此问题和其他问题之前我不建议将其用于任何人。

Personally until Q fixes this and other issues I cannot recommend using it to anyone.

我写了规范基于Domenic和Petka的工作,承诺库能够全局报告错误并挂钩它们。有几个库已经实现了这个,包括bluebird和when。 Domenic正致力于Web浏览器的并行规范。

I've written a specification based on work by Domenic and Petka for promise libraries to be able to report errors globally and hook to them. Several libraries already implement this including bluebird and when. Domenic is working on a parallel specification for web browsers.

目前支持或将在未来几周内得到支持的是:bluebird,when,es6-promise,rsvp和io中的本地承诺。

Currently supported or are going to be supported in the next few weeks are: bluebird, when, es6-promise, rsvp and native promises in io.

// log any unhandled promise rejections
process.on('unhandledRejection', function(reason, p){
    console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason);
    // application specific logging here
});

对于浏览器,例如:

window.addEventListener("unhandledrejection", function(e) {
    var reason = e.detail.reason;
    var promise = e.detail.promise;
    console.log("Unhandled rejection", promise, reason);
});

此协议支持较少,但计划将其包含在本机承诺中。目前Firefox原生承诺将报告未处理的拒绝,Chrome也将尝试 - 但目前还没有浏览器挂钩(虽然它即将到来)。

This protocol is less supported but there are plans to include it in native promises. Currently Firefox native promises will report unhandled rejections and Chrome's will attempt too - but there are no browser hooks for it yet (it's coming though).

请注意,团队正在努力非常有趣的工具。在与Paul Irish讨论之后,我确信在浏览器中调试承诺的工具方面会有很多好处,这将使本机承诺几乎像bluebird promises一样可调试(这太棒了!)。

Note that teams are working on very interesting tooling. After a discussion with Paul Irish I'm convinced great things are going to come our way in terms of tooling for debugging promises in browsers that will make native promises almost as debuggable as bluebird promises (which is awesome!).

这篇关于函数返回一个Promise,检查错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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