I need to implement in a chained promise function, any function error in the middle terminates the program, and can catch the error function, and then perform different operations according to different error functions. How can the following code be improved?
do not bring information in the error object
function f1(){
return new Promise((resolve,reject) =>{
setTimeout(() =>{
reject("err");
},1000)
})
}
function f2(){
return new Promise((resolve,reject) =>{
setTimeout(() =>{
reject("err");
},1000)
})
}
function f3(){
return new Promise((resolve,reject) =>{
setTimeout(() =>{
reject("err");
},1000)
})
}
const foo = () =>{
return f1().then(() =>{
return f2();
}).then(() =>{
return f3();
}).catch(err =>{
//
return err;
})
}
foo();
if every function sends an asynchronous request to the backend, the format of the data returned by the backend must be fixed, that is, we cannot judge which function threw the error based on the data returned by the backend. What should I do?
Thank you for your help!