Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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 Bluebird.js自定义错误捕获函数,不适用于第一个承诺?_Javascript_Error Handling_Promise_Bluebird - Fatal编程技术网

Javascript Bluebird.js自定义错误捕获函数,不适用于第一个承诺?

Javascript Bluebird.js自定义错误捕获函数,不适用于第一个承诺?,javascript,error-handling,promise,bluebird,Javascript,Error Handling,Promise,Bluebird,我正在尝试使用Bluebird.js的自定义错误处理程序 在下面的示例中,调用了catch all处理程序,而不是MyCustomError处理程序,但是当我将拒绝移动到then函数中(并解析了firstPromise…)时,调用了MyCustomError处理程序。 为什么呢?有什么不对劲吗?谢谢 var Promise = require('bluebird'), debug = require('debug')('main'); firstPromise() .then(func

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

在下面的示例中,调用了catch all处理程序,而不是MyCustomError处理程序,但是当我将拒绝移动到then函数中(并解析了firstPromise…)时,调用了MyCustomError处理程序。 为什么呢?有什么不对劲吗?谢谢

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;

在做任何事情之前声明错误类,它就会工作(原型作业不会被提升)

谢谢,还有蓝鸟!我遇到了完全相同的问题,但很明显,从你的回答中我什么都不明白:\n你能说清楚吗?@scaryguy highting意味着某个东西被移动到范围的顶部,就像它最初在那里声明的一样。在本例中,
MyCustomError
,作为一个函数声明(被提升),被移到顶部,但是伴随的原型分配没有得到相同的处理,因此代码不能像预期的那样工作。非常干净的从错误继承的方式,我现在使用它:)谢谢!