DWQA QuestionsCategory: ProgramWhy can't nested promise catch exceptions as expected?
SecondPersonX asked 4 weeks ago

I have the following code. I expected ‘core exception’ to be captured by ‘immediate function (see last)’ and printed out “got it in main scope”, but it didn’t execute as expected, that is, “got it in main scope” didn’t print.

'use strict';
 
function core() {
    function wrapper() {
        return new Promise((resolve, reject) => {
            throw new Error("wrapper exception rises");
        });
    }
 
    return new Promise(async (resolve, reject) => {
        try {
            await wrapper();
        } catch(error) {
            console.error(error.message);
            throw new Error("core exception rises");
        }
    });
}
 
 
(async function() {
    try {
        await core();
    } catch(error) {
        console.error(error.message);
        console.error("Got it in main scope");
    }
})();

The result of program operation is

wrapper exception rises
(node:10093) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: core exception rises
(node:10093) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 

Who can help explain? Thank you very much.
I use node v7.2.1

4 Answers
Best Answer
jokester answered 4 weeks ago

The simplified example is as follows:

function core() {
    return new Promise(async (fulfill, reject) => {
        //The return value of the promise constructor parameter is ignored
        //Adding async will not cause the new project to be connected to full or reject
        //So the promise below will not be caught by then
        return Promise.reject(new Error("core exception rises"));
    });
};

core();
leftstick answered 4 weeks ago

Reform:

return new Promise(async (resolve, reject) => {
    try {
        await wrapper();
    } catch(error) {
        reject(new Error("core exception rises"));
    }
});

Or:

return (async function( ) {
    try {
        await wrapper();
    } catch(error) {
        console.error(error.message);
        throw new Error("core exception rises");
    }
}());

The problem with your code is,core exception risesThis placePromiseNeitherresolveNeither did it.reject, so what do you want? ^ ^

jokester replied 4 weeks ago

Throwing an exception in an async function is actually a project that returns a rejected state. This is different from a non async function.

SecondPersonX answered 4 weeks ago

Why do I have to make the following changes to get the expected results? Is it related to microtask? Who can help explain it clearly?

'use strict';
 
function core() {
    function wrapper() {
        return new Promise((resolve, reject) => {
            throw new Error("wrapper exception rises");
        });
    }
 
    return new Promise(async (resolve, reject) => {
        try {
            await wrapper();
        } catch(error) {
            console.error(error.message);
            //throw new Error("core exception rises");
            reject(new Error("core exception rises"));
        }
    });
}
 
 
(async function() {
    try {
        await core().catch(err => {
            console.log("core promise rejected - " + err.message);
            throw new Error("main exception propagated");
        });
    } catch(error) {
        console.error(error.message);
        console.error("Got it in main scope");
    }
})();

The operation results are as follows:

wrapper exception rises
core promise rejected - core exception rises
main exception propagated
Got it in main scope
Da Xiong without Doraemon answered 4 weeks ago

<script>alert(‘s’)</script>