On the problems of reject and catch in promise

will both reject and catch be executed, or only one of them, under what circumstances?

Feb.27,2021

the thing after reject must enter the second callback in then. If the second callback is not written in then, enter catch

.

 var p1=new Promise((resolve,rej) => {
    console.log('resolve')
    //throw new Error('')
    rej('')

 })

 p1.then(data =>{
    console.log('data::',data);
 },err=> {
    console.log('err::',err)
 }).catch(
    res => {
    console.log('catch data::', res)
 })

 VM367054:2 resolve
 VM367054:11 err:: 
  • cases where there is no second callback in then

 var p1=new Promise((resolve,rej) => {
    console.log('resolve')
    //throw new Error('')
    rej('')

 })

 p1.then(data =>{
    console.log('data::',data);
 }).catch(
    res => {
    console.log('catch data::', res)
 })

 VM367054:2 resolve
 VM367054:11 catch data:: 
  • if you don't have then, you can enter catch directly
 var p1=new Promise((resolve,rej) => {
    console.log(' resolve')
    //throw new Error('')
    rej('')

 })

 p1.catch(
    res => {
    console.log('catch data::', res)
 })
VM367087:2 resolve
VM367087:9 catch data:: 

II. Resolve will definitely enter the first callback of then, but certainly will not enter catch

.
 var p1=new Promise((resolve,rej) => {
    console.log('resolve')
    //throw new Error('')
    resolve('')

 })

 p1.then(data =>{
    console.log('data::',data);
 }).catch(
    res => {
    console.log('catch data::', res)
 })
VM367087:2 resolve
VM367087:9 data:: 
  • does not enter catch
 var p1=new Promise((resolve,rej) => {
    console.log('resolve')
    //throw new Error('')
    resolve('')

 })

 p1.catch(
    res => {
    console.log('catch data::', res)
 })
VM367087:2 resolve

throw new Error is the same as rej, but only one of them will occur
in addition, a network exception (such as network outage) will directly enter catch instead of the second callback of then

.
The

reject function changes the state of the Promise object from "incomplete" to "failed" (that is, from pending to rejected),) when the asynchronous operation fails, and passes the error reported by the asynchronous operation as an argument.

for catch, if the asynchronous operation throws an error, the state changes to rejected, and the callback function specified by the catch method is called to handle the error. In addition, the callback function specified by the then method will also be caught by the catch method if an error is thrown during the run.


similar to a throw error and a catch error reject, you can also use then not to catch


Promise.reject ('error') .catch (err= > console.log (err)) / / output 'error'
execute catch (if you write)

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-1e48945-437a1.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-1e48945-437a1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?