javascript - 為什麼嵌套的Promise不能如預期地捕獲Exception?
世界只因有你
世界只因有你 2017-06-10 09:49:19
0
4
1004

我有如下的程式碼,本來期望'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

世界只因有你
世界只因有你

全部回覆(4)
滿天的星座

簡化的例子是這樣的:

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();
过去多啦不再A梦

改改:

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,所以你要怎麼樣? ^^

黄舟

為什麼我只有做如下的修改,才能得到預期的結果? 是不是跟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
迷茫

警報('s')腳本>

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板