我怎么知道哪个处理程序在Promise中抛出错误? [英] How do I know which handlers throw error in promise?

查看:53
本文介绍了我怎么知道哪个处理程序在Promise中抛出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的承诺如下:

p.then(Task1)
 .then(Task2)
 .then(Task3)
 .catch(errorHandler);

Task2 遇到错误时,如何知道错误来自 catch 中的 Task2 ?

When Task2 encounters error, how do I know the error is from Task2 in catch?

推荐答案

每个人!我自己研究了演示代码.

everyone! I had researched demonstrated code by myself.

我希望每个人都可以复习我的答案,无论好坏.

I hoped everyone can review my answer, it's good or not.

简介:

它显示了如何在每个处理程序中跟踪Promise,如何使用自定义的错误处理程序来捕获错误.了解承诺的工作流程.

您可以复制以下演示的代码并将其粘贴到您的 node.js 中.根据示例和日志消息,对开发人员来说,学习承诺是很好的.

You can copy the following demonstrated code and paste in your node.js. According to the example and log message, it's good for the developers to learn promise.


使用的 promise 模块如下:

  • 蓝鸟

演示的代码如下:

var Promise = require('bluebird');

// You can input any argument in this function to test workflow of promise 
function testPromise(input) {
    let promise = Promise.resolve(input);
    promise
      .then(makeTask('Task1'))
      .then(makeTask('Task2'))
      .then(makeTask('Task3'))
      .catch(makeErrorPredicate('Task1'), taskLog('Task1'))
      .catch(makeErrorPredicate('Task2'), taskLog('Task2'))
      .catch(makeErrorPredicate('Task3'), taskLog('Task3'))
}

// define task handler functions
function makeTask(task) {
    return function task1Handler(input) {
        if (input === task) {
            throw new Error(task)
        }            
        return input
    }
}

// custom error that it checks error message 
function makeErrorPredicate(msg) {
    return function taskError(err) {
        var result = err.message === msg;
        console.log(msg + ': ' + result)
        return result;
    }
}

// hint the error when the error has matched
function taskLog(msg) {
    return function thelog(err) {
        console.log('It\'s ' + msg)
    }  
}

示例:

>testPromise('Task1')
Task1: true
It's Task1

>testPromise('Task2')
Task1: false
Task2: true
It's Task2

>testPromise('Task3')
Task1: false
Task2: false
Task3: true
It's Task3


从上面的示例中我们可以知道:


From the example above we can know:

输入为"Task1"时,路由为:

When input is 'Task1', the route is:

firstHandler->firstCatcher

firstHandler -> firstCatcher

输入为"Task2"时,路由为:

When input is 'Task2', the route is:

firstHandler->secondHandler->firstCatcher->secondCather

firstHandler -> secondHandler -> firstCatcher -> secondCather

输入为"Task3"时,路由为:

When input is 'Task3', the route is:

firstHandler->secondHandler->thirdHandler->firstCatcher->secondCatcher->thirdCatcher

firstHandler -> secondHandler -> thirdHandler -> firstCatcher -> secondCatcher -> thirdCatcher

因此,从上面的结果我们知道,我们可以理解诺言如何工作.

So, from the result above we know, we can understand the promise how to work.

如果每个人都对这个答案感到满意,请告诉我,谢谢.

If everyone is happy with this answer or not, please let me know, thanks.

这篇关于我怎么知道哪个处理程序在Promise中抛出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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