Bluebird.js自定义错误捕获功能,不适用于第一个承诺? [英] Bluebird.js custom Error catch function, does not apply on the first promise?

查看:100
本文介绍了Bluebird.js自定义错误捕获功能,不适用于第一个承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Bluebird.js的自定义错误处理程序。

I'm trying to use the custom error handlers of Bluebird.js.

在下面的示例中,调用catch-all处理程序,而不是MyCustomError处理程序,但是当我将拒绝转移到然后函数(并解决了第一个Promise ...)时,MyCustomError处理程序被调用。
为什么?有什么问题吗谢谢。

In the example bellow the catch-all handler is called, not the MyCustomError handler, but when I moved the rejection into the then function (and resolved the firstPromise...), the MyCustomError handler is called. Why is that? got something wrong? Thanks.

var Promise = require('bluebird'),
debug = require('debug')('main');

firstPromise()
    .then(function (value) {
      debug(value);
    })
    .catch(MyCustomError, function (err) {
      debug('from MyCustomError catch: ' + err.message);
    })
    .catch(function (err) {
      debug('From catch all: ' + err.message);
    });

/*
 * Promise returning function.
 * */
function firstPromise() {
  return new Promise(function (resolve, reject) {
    reject(new MyCustomError('error from firstPromise'));
  });
}
/*
 *  Custom Error
 * */
function MyCustomError(message) {
  this.message = message;
  this.name = "MyCustomError";
  Error.captureStackTrace(this, MyCustomError);
}
MyCustomError.prototype = Object.create(Error.prototype);
MyCustomError.prototype.constructor = MyCustomError;


推荐答案

在任何其他方式声明错误类,它将工作(原型作业没有悬挂)

Declare the error class before anything else and it will work (prototype assignments are not hoisted)

这篇关于Bluebird.js自定义错误捕获功能,不适用于第一个承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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