try catch can't catch promise error

The reference link
try catch is not a panacea, and it cannot catch promise errors. However, promises can be followed by catches to catch errors. If there are multiple layers of promises, catches must be added for each layer.
code example

// 捕获失败
try {
    
    
    new Promise((resolve,reject)=>{
    
    
        throw Error(1)
    })
} catch (e) {
    
    
    console.error('catch error')
}

// 能捕获
try {
    
    
    new Promise((resolve,reject)=>{
    
    
        throw Error(1)
    }).catch(e=>{
    
    
        console.error('error catch!')
    })
} catch (e) {
    
    
    console.error('catch error', e)
}

// 多层 不能
try {
    
    
    new Promise((resolve,reject)=>{
    
    
        return new Promise(()=>{
    
    
            throw new Error(1)
        })
    }).catch(e=>{
    
    
        console.error('error catch!')
    }
    )
} catch (e) {
    
    
    console.error('catch error', e)
}

// 每层都catch,然后reject外抛,能捕获
try {
    
    
    new Promise((resolve,reject)=>{
    
    
        return new Promise(()=>{
    
    
            throw new Error(1)
        }).catch(e=>{
    
    
            return reject(e)
        })
    }).catch(e=>{
    
    
        console.error('error catch!')
    }
    )
} catch (e) {
    
    
    console.error('catch error', e)
}

What is the principle here?
Refer to here , there is a try catch by default in the promise executor (here), and when there is an exception, it will execute reject and use the exception as a parameter.

We know that try catch can catch the exception of the try code block, but if the exception is thrown in the promise, the exception is not executed immediately, but reject is called, and reject is added to the microtask for execution. For details, you can learn about javascript event loop mechanism
such as the following code

console.log(1)
new Promise((resolve) => {
    
    
  console.log(2)
  throw Error(4)
}).then((e) => {
    
     console.log(e) })
console.log(3)

After outputting 1, 2, 3, exception 4 is thrown. By the time of exception 4, it is no longer in the marcotask of the try catch. The part in the try catch has been executed, but the exception is processed in the microtask, so it cannot be caught

console.log(1)
new Promise((resolve) => {
    
    
  console.log(2)
  throw Error(4)
}).catch(e=>{
    
    console.warn(e)})
console.log(3)

So this captures

this is okay too

new Promise(r=>{
    
    

    try{
    
    eee} catch(e){
    
    console.warn(e)}
})
// ReferenceError: eee is not defined

But this doesn’t work. Similarly, if setTimeout is executed asynchronously, the code in try has actually finished running.

try {
    
    
    setTimeout(()=>{
    
    eee})
}catch(e) {
    
    

    console.warn(e)
}

There is another method that can also be captured, plus await

async function getSomeValue() {
    
    
    throw new Error('uh oh');
}

async function a() {
    
    
  try {
    
    
    const someValue = await getSomeValue();
  } catch (error) {
    
    
    console.warn(error);
  }
}

Await is to wait for the promise to resolve or reject before continuing to execute the code, so why can it be caught by adding a try catch outside? Is it because the execution is back to macrotask?

Guess you like

Origin blog.csdn.net/ZhaoBuDaoFangXia/article/details/131920143
Recommended