will both reject and catch be executed, or only one of them, under what circumstances?
will both reject and catch be executed, or only one of them, under what circumstances?
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::
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::
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::
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::
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
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)
Previous: After starting the node.js server, does it have to be refreshed before the terminal can have output?
Next: What is the principle of cross-domain proxy proxyTable in node?