javascript - Node.js await catch not returning - Stack Overflow

I am busy working with some code that is responing in an unexpected way (to me). It involves handling N

I am busy working with some code that is responing in an unexpected way (to me). It involves handling Node.js promise exceptions.

I have the following function modified so that all it does is fail

function asynFunc() {
    return new Promise(function(res, rej) {

        rej("GENERIC ERROR");

    });
}

The problem es in when I am trying to call it and handle this error. If I do the following it works as I expect it to, the function fails and executes the catch which returns, stopping the last console log from executing

async function appStart() {

    try {
        await asyncFunc();
    } catch(log) {
        console.log(log);
        return;
    }

    console.log("YOU SHOULD NOT SEE THIS");

}

appStart();

If I write the function as follows though, it does not seem to return, it still executes the last console log regardless of the await

async function appStart() {

    await asyncFunc().catch(function(log) {
        console.log(log);
        return;
    });

    console.log("YOU SHOULD NOT SEE THIS");

}

If this is doing what I think it's doing then the return is returning from the function inside of the catch and not the appStart function itself, or I'm pletely wrong in which case I have no idea why it's not returning.

Is there a way to use the second catch method and still have it return from the calling function?

I am busy working with some code that is responing in an unexpected way (to me). It involves handling Node.js promise exceptions.

I have the following function modified so that all it does is fail

function asynFunc() {
    return new Promise(function(res, rej) {

        rej("GENERIC ERROR");

    });
}

The problem es in when I am trying to call it and handle this error. If I do the following it works as I expect it to, the function fails and executes the catch which returns, stopping the last console log from executing

async function appStart() {

    try {
        await asyncFunc();
    } catch(log) {
        console.log(log);
        return;
    }

    console.log("YOU SHOULD NOT SEE THIS");

}

appStart();

If I write the function as follows though, it does not seem to return, it still executes the last console log regardless of the await

async function appStart() {

    await asyncFunc().catch(function(log) {
        console.log(log);
        return;
    });

    console.log("YOU SHOULD NOT SEE THIS");

}

If this is doing what I think it's doing then the return is returning from the function inside of the catch and not the appStart function itself, or I'm pletely wrong in which case I have no idea why it's not returning.

Is there a way to use the second catch method and still have it return from the calling function?

Share Improve this question asked Aug 10, 2018 at 15:55 TheLovelySausageTheLovelySausage 4,14417 gold badges67 silver badges116 bronze badges 1
  • related stackoverflow./q/55378850/133327 – abernier Commented Dec 29, 2019 at 16:51
Add a ment  | 

2 Answers 2

Reset to default 6

In the second example, you are not returning from the outside function in the catch, you are returning from the catch callback:

await asyncFunc().catch(function(log) {
    console.log(log);//  ^
    return;          //  | returns from that function
});

This has the effect of catching the error and moving on and returning a new promise resolving to undefined. There is no way to control the return of the outside function from inside the callback. You need to test the result of the async operation from the outside function, which leaves you with try/catch or explicitly testing the result after the promise resolves.

You're right. It's returning only inside the catch callback, not the outer async function. That's why it exits the catch callback and resolves, and then logs "YOU SHOULD NOT SEE THIS". Generally, it's very unreadable if you mix promise then and catch chaining along with async/await and try/catch. Pick one and stick with it, because mixing them may lead to the inability to catch and handle errors seamlessly.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744937923a4602140.html

相关推荐

  • javascript - Node.js await catch not returning - Stack Overflow

    I am busy working with some code that is responing in an unexpected way (to me). It involves handling N

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信