承诺 - 在deferred.reject期间包装错误

时间:2022-02-18 18:50:45

I'm trying to create a deferred wrapper using q.js such that I can wrap errors (strings) in a custom error class before they're passed back by the promise in then() or fail(). This is what I'm doing at the moment:

我正在尝试使用q.js创建一个延迟包装器,这样我就可以将错误(字符串)包装在自定义错误类中,然后再将它们传递给then()或fail()中的promise。这就是我现在正在做的事情:

var getDeferred = function() {

    var deferred = q.defer();

    var reject = deferred.reject;
    deferred.reject = function(error) {

        if (!(error instanceof MyErrorClass))
            error = new MyErrorClass(error)

        return reject.apply(deferred, arguments);
    }

    return deferred;
}

So the idea is that the user would do something like

所以想法是用户会做类似的事情

var deferred = getDeferred();

deferred.promise.fail(function(err) {
    // err should now be instance of MyErrorClass and NOT a string
})

deferred.reject('A string error')

And expect to get MyErrorClass in the fail() handler, rather than the string passed to deferred.reject.

并期望在fail()处理程序中获取MyErrorClass,而不是传递给deferred.reject的字符串。

The above code works, but it's hardly ideal -- I know I shouldn't be monkey-patching deferred.reject. But is there a better way to do this?

上面的代码可以工作,但它并不理想 - 我知道我不应该使用猴子修补deferred.reject。但是有更好的方法吗?

2 个解决方案

#1


1  

It's prettier / more promise oriented like this :

这是更漂亮/更承诺这样的:

var getDeferred = function() {

    var deferred = q.defer();

    deferred.promise = deferred.promise.then(null, function(error) {
        if (!(error instanceof MyErrorClass))
            error = new MyErrorClass(error)

        throw error
    }

    return deferred;
}

This way you just attach an error handler which will mutate any non MyErrorClass errors. It seems like an odd use case in general though...

这样您只需附加一个错误处理程序,它将改变任何非MyErrorClass错误。虽然看起来像一个奇怪的用例...

#2


0  

Simply map over the fail case with then:

只需映射失败案例即可:

actualPromise.then(null, function(error) {
    if (!(error instanceof MyErrorClass))
        error = new MyErrorClass(error)
    throw error;
})
.fail(function(err) {
    // err is now an instance of MyErrorClass and NOT a string
});

If you need a wrapper functionality, use a function that takes the actualPromise and calls then with the above code on it.

如果您需要一个包装器功能,请使用一个带有actualPromise的函数,然后使用上面的代码调用它。

#1


1  

It's prettier / more promise oriented like this :

这是更漂亮/更承诺这样的:

var getDeferred = function() {

    var deferred = q.defer();

    deferred.promise = deferred.promise.then(null, function(error) {
        if (!(error instanceof MyErrorClass))
            error = new MyErrorClass(error)

        throw error
    }

    return deferred;
}

This way you just attach an error handler which will mutate any non MyErrorClass errors. It seems like an odd use case in general though...

这样您只需附加一个错误处理程序,它将改变任何非MyErrorClass错误。虽然看起来像一个奇怪的用例...

#2


0  

Simply map over the fail case with then:

只需映射失败案例即可:

actualPromise.then(null, function(error) {
    if (!(error instanceof MyErrorClass))
        error = new MyErrorClass(error)
    throw error;
})
.fail(function(err) {
    // err is now an instance of MyErrorClass and NOT a string
});

If you need a wrapper functionality, use a function that takes the actualPromise and calls then with the above code on it.

如果您需要一个包装器功能,请使用一个带有actualPromise的函数,然后使用上面的代码调用它。