搜索

首页  >  问答  >  正文

javascript - 为什么嵌套的Promise不能按预期捕获Exception?

我有如下的代码,本来期望‘core exception’ 可以被 ‘立即执行的函数(见最后)’捕获到,并打印出“Got it in main scope”,但没有按预期执行,即"Got it in main scope"没有打印。

'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");
    }
})();

程序运行结果为

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.
 

谁帮忙解释一下?非常感谢!

我使用的是node v7.2.1

世界只因有你世界只因有你2809 天前1008

全部回复(4)我来回复

  • 滿天的星座

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

    简化的例子是这样的:

    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();

    回复
    0
  • 过去多啦不再A梦

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

    改改:

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

    或者:

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

    你代码的问题是,core exception rises这个地方的Promise既没有resolve,也没有reject,所以你要怎样呢?^^

    回复
    0
  • 黄舟

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

    为什么我只有做如下的修改,才能得到预期的结果呢? 是不是和microtask有关呢?谁能帮忙清楚地解释一下?

    '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");
        }
    })();

    得到的运行结果为:

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

    回复
    0
  • 迷茫

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

    <脚本>警报('s')</脚本>

    回复
    0
  • 取消回复