undefinedfix
Sign in

Async / await catch not found error?

Cal edited in Tue, 23 Aug 2022
function f() {
    return new Promise((resolve, reject) => {
        setTimeout(()=>{
            reject(0)
        },1000)
    })
}
async function  main() {
    let func=[];
    try {
        func.push(f());
        for (let i of [1,2,3]){
            await f();
        }
        await Promise.all(func);
    }catch (e) {
        console.log(e);
    }
}
main();

Why does this report unhandled project rejection? Is f () executed when it is put into func

2 Replies
Jim
commented on Tue, 23 Aug 2022

because of you func.push (f ()) then of course it will be executed. F () you don't want to execute that func.push (f) If you want to catch, use f (). Catch ()

user282
commented on Tue, 23 Aug 2022

func.push (f ()) this sentence throws an error. When f () is called, there is no await. If it is changed to the following, no error will be reported:

func.push(f)
// 或
func.push(await f())