Testing Out Promise.All Catch, Await, Async

  • #promise catch err, catch returns nothing, b:undefined, logged er. throw must be out of settimeout body
    async function get(){return new Promise((res,rej)=>{ throw "er";setTimeout(()=> {console.log(3);res(2)},3000)});}
    var b= await Promise.all([get()]).catch(er=>console.log(er))
  • #promise.all return value, an array of promises resolve [2] below.
    async function get(){return new Promise((res,rej)=>{setTimeout(()=>{console.log(3);res(2)},3000)});}
    var b=await Promise.all([get()])
  • #await no promise, output: directly 7
    await console.log(7);
  • #await no effect without promise, output, b:8
    function g1(){return 8;}
    var b=await g1(); #one promise errors out, output, a:undefined
    async function get(){return new Promise((res,rej)=>{setTimeout(()=>{console.log(3);res(2)},3000)});}
    async function get1(){return new Promise((res,rej)=>{ throw "er";setTimeout(()=>{console.log(3);res(2)},3000)});}
    var a=Promise.all([get(),get1()]).catch((e)=>console.log(e))
    var a=await Promise.all([get(),get1()]).catch((e)=>console.log(e))
  • #promise.all no error, with catch or not, both output: a:[2,3]
    async function get(){return new Promise((res,rej)=>{setTimeout(()=>{console.log(3);res(2)},3000)});}
    async function get1(){return new Promise((res,rej)=>{ setTimeout(()=>{console.log(3);res(3)},3000)});}
    var a=await Promise.all([get(),get1()])
    var a=await Promise.all([get(),get1()]).catch((e)=>console.log(e))


留下你的评论