Try/catch was unable to capture the problem with promise.reject

function f2() {
  try {
    Promise.reject("");
  } catch(e) {
    console.log(e)
  }
}
  • execute f2 () , and Uncaught (in promise) cannot be thrown from the promise.reject, console captured by try/catch.
async function f() {
  try {
    await Promise.reject("")
  } catch(e) {
    console.log(e)
  }
}
  • Why after changing to await/async, execute f () to catch the error in catch and not throw Uncaught (in promise)
Mar.14,2021

it should be understood that the < del > rejection occurs in the future < / del > callback will only be executed in the future event loop.

function f2() {
  try {
    Promise.reject('').catch(err => {
      console.log('2', err)
    });
    console.log('1')
  } catch (e) {
    console.log(e)
  }
}

try..catch structure, which can only be Synchronize, can not be used in asynchronous code mode


to answer this question, the principle or explore what exactly await does?
await: means pausing the asynchronous function promise, to wait for the execution of any expression.
for the f2 function, Promise.reject ('error') executes asynchronously, but there is no catch function for reject processing.
for the f function, await Promise.reject ('something went wrong'). It is equivalent to adding a catch processing method to the end, which returns the incoming 'error' information, so it does not report an error.


because
1, try catch cannot catch asynchronous code errors
2, Promise.reject is an asynchronous method


21st century, use native async and promise

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38af9-2c116.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38af9-2c116.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?