最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

捕获使用异步错误等待 的异步和同步功能

IT培训 admin 11浏览 0评论

捕获使用异步错误/等待 的异步和同步功能

我有一个需要能够同时调用异步功能以及同步功能的路由器。我已经设置了下面的代码执行人:

async exec(method, path, params, payload) {
    const route = this.get(method, path);

    if (!route) throw new RequestError(422, 'Route [' + path + '] not found');

    // Recheck, just to make sure
    if (typeof route._callback !== 'function')
        throw new AppError('Callback defined for route [' + route.name + '] is not a function');

    this._logger('info', 'Route [' + path + '] recieved.  Executing [' + route.callback + ']', route.params);

    let results;
    try {
        results = route._callback(Object.assign(params || {}, route.params || {}), payload);
        // If the results is a promise, then we await the promise results
        if (results.then && typeof results.then === 'function') {
            await results;
        }
    } catch (err) {
        throw new AppError(err.message, err);
    }

    return results;
}

在调用函数,我有哪里有可以引发错误两种不同的潜在领域多种功能。一个是Promise.All之外,另一个是内部。

async myFunction(params,payload) {

    // This function can throw an error if the database 
    // connection goes bad
    if( await this._checkExists(this.conn, params) )
        return { code: 2, message: 'Already Exists' };

    if ((await this._regCount(this.conn, params)) === 0) {
        Promise.all([
            this._sendMail(params), 
            this._notify(params),
            this._update(params)
        });

        return { code: 1, message: 'Success' };
    }
}

由于路由器是一个库的一部分,myFunction的是,我没有控制用户的功能,我想路由器能够赶上myFunction的范围内发生的任何异常(或其任何内部函数)。我不希望用户被强迫写try / catch块内部。我希望他们能泡到路由器内部的错误报告功能。

在当前的代码结构,如果我扔内myFunction的错误,然后在路由器的exec功能捕捉工作正常。然而,如果误差内的任何的内部功能抛出(即_checkExists_sendMail)等,然后我得到UnhandledRejection错误。

什么是正确的方法来捕获此路由器的exec函数内?

回答如下:

路由器代码是好的。我结束了重新构建myFunction的是:

async myFunction(params,payload) {

    // This function can throw an error if the database 
    // connection goes bad
    if( await this._checkExists(this.conn, params) )
        return { code: 2, message: 'Already Exists' };

    if ((await this._regCount(this.conn, params)) > 0)
        return { code: 1, message: 'Already Exists' };

    Promise.all([
        this._sendMail(params), 
        this._notify(params),
        this._update(params)
    });

    return { code: 1, message: 'Success' };
}

这我也不清楚为什么它会有所作为。当然,上面的代码会不返回任何东西,如果有在regCount抛出异常,但不知道为什么会冒泡到UnhandledRejection。

问题已经解决,但异步/等待似乎是相当挑剔的,如果你不明白这一点恰到好处,你可以运行与处理的承诺的问题。

RANT:如果ECMA只想解决的能够运行多线程,而不是试图在一个单独的线程一切工作的核心问题,我们就不必经历这些痛苦异步。给我们的线程,我们可以选择何时何地,我们想与同步异步。

....好吧,步进从我的肥皂盒下来。

捕获使用异步错误/等待 的异步和同步功能

我有一个需要能够同时调用异步功能以及同步功能的路由器。我已经设置了下面的代码执行人:

async exec(method, path, params, payload) {
    const route = this.get(method, path);

    if (!route) throw new RequestError(422, 'Route [' + path + '] not found');

    // Recheck, just to make sure
    if (typeof route._callback !== 'function')
        throw new AppError('Callback defined for route [' + route.name + '] is not a function');

    this._logger('info', 'Route [' + path + '] recieved.  Executing [' + route.callback + ']', route.params);

    let results;
    try {
        results = route._callback(Object.assign(params || {}, route.params || {}), payload);
        // If the results is a promise, then we await the promise results
        if (results.then && typeof results.then === 'function') {
            await results;
        }
    } catch (err) {
        throw new AppError(err.message, err);
    }

    return results;
}

在调用函数,我有哪里有可以引发错误两种不同的潜在领域多种功能。一个是Promise.All之外,另一个是内部。

async myFunction(params,payload) {

    // This function can throw an error if the database 
    // connection goes bad
    if( await this._checkExists(this.conn, params) )
        return { code: 2, message: 'Already Exists' };

    if ((await this._regCount(this.conn, params)) === 0) {
        Promise.all([
            this._sendMail(params), 
            this._notify(params),
            this._update(params)
        });

        return { code: 1, message: 'Success' };
    }
}

由于路由器是一个库的一部分,myFunction的是,我没有控制用户的功能,我想路由器能够赶上myFunction的范围内发生的任何异常(或其任何内部函数)。我不希望用户被强迫写try / catch块内部。我希望他们能泡到路由器内部的错误报告功能。

在当前的代码结构,如果我扔内myFunction的错误,然后在路由器的exec功能捕捉工作正常。然而,如果误差内的任何的内部功能抛出(即_checkExists_sendMail)等,然后我得到UnhandledRejection错误。

什么是正确的方法来捕获此路由器的exec函数内?

回答如下:

路由器代码是好的。我结束了重新构建myFunction的是:

async myFunction(params,payload) {

    // This function can throw an error if the database 
    // connection goes bad
    if( await this._checkExists(this.conn, params) )
        return { code: 2, message: 'Already Exists' };

    if ((await this._regCount(this.conn, params)) > 0)
        return { code: 1, message: 'Already Exists' };

    Promise.all([
        this._sendMail(params), 
        this._notify(params),
        this._update(params)
    });

    return { code: 1, message: 'Success' };
}

这我也不清楚为什么它会有所作为。当然,上面的代码会不返回任何东西,如果有在regCount抛出异常,但不知道为什么会冒泡到UnhandledRejection。

问题已经解决,但异步/等待似乎是相当挑剔的,如果你不明白这一点恰到好处,你可以运行与处理的承诺的问题。

RANT:如果ECMA只想解决的能够运行多线程,而不是试图在一个单独的线程一切工作的核心问题,我们就不必经历这些痛苦异步。给我们的线程,我们可以选择何时何地,我们想与同步异步。

....好吧,步进从我的肥皂盒下来。

发布评论

评论列表 (0)

  1. 暂无评论