描述
我写了一个简单的附录:
declare global {
interface Promise<T> {
isFulfilled: Promise<boolean>;
}
}
Object.defineProperty(Promise.prototype, "isFulfilled", {
async get<T>(this: Promise<T>): Promise<boolean> {
const symbol = Promise.resolve(Symbol());
try {
const result = (await Promise.race([this, symbol]) !== symbol);
console.log("after promise");
return result;
} catch {
console.log(`catch`);
return true;
}
}
});
const promise = new Promise<void>(async (resolve) => {
throw new Error(`Test error`);
});
console.log(await (promise.isFulfilled));
export { };
我们看到,Promise 的执行发生在 try 中,并没有超越它。这种情况下,如果出现错误,try应该捕获它,并去catch。
我收到一个错误并且尝试继续:
问题
为什么会发生这种情况?
我该如何修复我的功能?