相关疑难解决方法(0)

为什么我不能扔进Promise.catch处理程序?

为什么我不能Error在catch回调中抛出一个内部并让进程处理错误,就好像它在任何其他范围内一样?

如果我什么都不做console.log(err)就会被打印出去,我对发生的事情一无所知.这个过程刚刚结束......

例:

function do1() {
    return new Promise(function(resolve, reject) {
        throw new Error('do1');
        setTimeout(resolve, 1000)
    });
}

function do2() {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            reject(new Error('do2'));
        }, 1000)
    });
}

do1().then(do2).catch(function(err) {
    //console.log(err.stack); // This is the only way to see the stack
    throw err; // This does nothing
});
Run Code Online (Sandbox Code Playgroud)

如果回调在主线程中执行,为什么Error会被黑洞吞噬?

javascript asynchronous throw promise es6-promise

119
推荐指数
3
解决办法
7万
查看次数

如何处理承诺错误?

作为节点程序员.我习惯使用"nodebacks"来处理我的代码中的错误:

myFn(param, function(err, data) {
    if (err){
        //error handling logic
     }
     else {
        // business logic
    }
});
Run Code Online (Sandbox Code Playgroud)

在编写该函数时,我可以执行以下操作:

var myFn = function(param, callback){
    var calc = doSomeCalculation(param);
    if(calc === null) { // or some other way to detect error
        callback(new Error("error with calculation"), null);
    }
    ...
    someAsyncOp(calcN,function(err, finalResult){
        if(err) return callback(err, null);
        callback(null, finalResult); // the error is null to signal no error
    });

};
Run Code Online (Sandbox Code Playgroud)

我如何使用promises进行这种错误处理?

javascript error-handling node.js promise bluebird

22
推荐指数
1
解决办法
4万
查看次数

如何拒绝(并正确使用)承诺?

短篇小说:

  • 谈到Promises/A +,拒绝承诺的正确方法是什么 - 抛出错误?但如果我错过了catch- 我的整个应用程序将会爆炸!

  • 如何使用promisify它有什么好处(也许你需要阅读更长的版本)?

  • .then(success, fail)真正的反模式,我应该使用.then(success).catch(error)

更长的版本,被描述为现实生活中的问题(希望有人阅读):

我有一个使用Bluebird(A + Promise实现库)的库,用于从数据库中获取数据(谈论Sequelize).每个查询都返回一个结果,但有时候它是空的(试图选择一些东西,但没有任何查询).承诺进入result函数,因为没有错误的原因(没有结果不是错误).例:

Entity.find(1).then(function(result) {
  // always ending here, despite result is {}
}).catch(function(error) {
  // never ends here
});
Run Code Online (Sandbox Code Playgroud)

我想包装它并检查结果是否为空(在我的代码中无法检查到这一点).我这样做了:

function findFirst() {
  return Entity.find(1).then(function(result) {
    if (result) { // add proper checks if needed
      return result; // will invoke the success function
    } else {
      // I WANT …
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise bluebird

10
推荐指数
2
解决办法
8685
查看次数

我可以解雇并忘记nodejs(ES7)中的承诺吗?

我想用babel运行这段代码:

redisClientAsync.delAsync('key');
return await someOtherAsyncFunction();
Run Code Online (Sandbox Code Playgroud)

在没有等待第一行的异步函数内部.这个可以吗?

我怎么能运行一些我不在乎的东西?

我可以在没有回调的情况下触发非promisified函数del('key',null)吗?

javascript promise async-await es6-promise ecmascript-7

8
推荐指数
2
解决办法
6040
查看次数

Node.js最佳实践异常处理 - 异步/等待之后

他们已经对这个话题提出了疑问

Node.js最佳实践异常处理

这是旧的,答案已经过时,domains甚至已经被弃用了.

现在在Async/Await Node.js之后的场景中,我们不应该同样考虑同步和异步情况,并在同步函数中抛出异常并拒绝异步函数中的promise,而不是Error在前一种情况下返回实例.

let divideSync = function(x,y) {
    // if error condition?
    if ( y === 0 ) {
        // "throw" the error 
        throw new Error("Can't divide by zero exception")
    }
    else {
        // no error occured, continue on
        return x/y
    }
}
Run Code Online (Sandbox Code Playgroud)

模拟异步除法运算

let divideAsync = function(x, y) {

  return new Promise(function(resolve, reject) {

    setTimeout(function() {
      // if error condition?
      if (y === 0) {
        // "throw" the error safely by rejecting the …
Run Code Online (Sandbox Code Playgroud)

javascript node.js async-await ecmascript-next

8
推荐指数
1
解决办法
5023
查看次数

Express中间件无法捕获async/await引发的错误,但为什么呢?

这两个中间件功能表现不同,我无法弄清楚原因:

在这里,错误将被try/catch困住:

router.get('/force_async_error/0',  async function (req, res, next) {
  try{
    await Promise.reject(new Error('my zoom 0'));
  }
  catch(err){
    next(err);
  }
});
Run Code Online (Sandbox Code Playgroud)

但是在这里,错误不会被try/catch困住:

router.get('/force_async_error/1', async function (req, res, next) {
  await Promise.reject(new Error('my zoom 1'));
});
Run Code Online (Sandbox Code Playgroud)

我认为Express用try/catch包装了所有中间件函数,所以我看不出它会有什么不同的行为?

我查看了Express源代码,处理程序看起来像:

Layer.prototype.handle_request = function handle(req, res, next) {
  var fn = this.handle;

  if (fn.length > 3) {
    // not a standard request handler
    return next();
  }

  try {
    fn(req, res, next); // shouldn't this trap the async/await error?
  } catch (err) {
    next(err); …
Run Code Online (Sandbox Code Playgroud)

node.js express async-await es6-promise express-4

5
推荐指数
2
解决办法
1294
查看次数