search

Home  >  Q&A  >  body text

Why doesn't nested Promise catch Exception as expected?

I have the following code. I expected that the 'core exception' could be caught by the 'immediately executed function (see the end)' and printed out "Got it in main scope", but it was not executed as expected, that is, "Got it in main scope" is not printed.

'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 program running result 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 so much!

I am using node v7.2.1

世界只因有你世界只因有你2728 days ago934

reply all(4)I'll reply

  • 滿天的星座

    滿天的星座2017-06-10 09:51:19

    A simplified example is this:

    function core() {
        return new Promise(async (fulfill, reject) => {
            // Promise constructor的参数的返回值是被无视的
            // 加个async不会导致新Promise被连接到fulfill或reject
            // 所以下面的那个Promise不会被then或catch,成为你看到的现象
            return Promise.reject(new Error("core exception rises"));
        });
    };
    
    core();

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-10 09:51:19

    Change:

    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 that core exception risesThe Promise in this place has neither resolve nor reject, so what do you do? ^^

    reply
    0
  • 黄舟

    黄舟2017-06-10 09:51:19

    Why can I get the expected results only by making the following modifications? Is it related to microtask? Can anyone 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 result of the operation is:

    wrapper exception rises
    core promise rejected - core exception rises
    main exception propagated
    Got it in main scope

    reply
    0
  • 迷茫

    迷茫2017-06-10 09:51:19

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

    reply
    0
  • Cancelreply