onError不能捕获unhandledrejection
发布于 6 年前 作者 pengjun 4024 次浏览 来自 问答

我在Promise的then回调中抛出一个error,没有使用catch处理:

function timeout(ms)
{
    return new Promise((resolve) =>
    {
        setTimeout(resolve, ms);
    });
}
 
timeout(100).then(() =>
{
    throw new Error("test");
});

console的报错截图是这样的:

我发现onError方法不能捕获未处理的Promise错误,即unhandledrejection。

那我应该怎样怎样全局监听unhandledrejection呢?

回到顶部