Ok, I like(d) try/catch with await/async.
But what do I do with this.
I wanted to do this..
let a = await doAbc();
let b = await do123(a);
what it becomes instead is
let a, b;
try {
a = await doAbc();
} catch(e) {
a = await doZxc();
}
try {
b = await do123(a);
} catch (e) {
console.log(e);
return;
}
if (b.data == undefined) {
return -1;
} else {
return b;
}
At this point I'm regretting everything.
Remember that you can await
any promise. So you could do:
let a = await doAbc().catch(doZxc); // or .catch(() => doZxc())
let b = await do123(a);
Or even
let b = await doAbc().catch(doZxc).then(do123);
Together with the rest of your code:
try {
let b = await doAbc().catch(doZxc).then(do123);
return b.data == undefined ? -1 : b;
} catch (e) {
console.log(e);
return;
}
If it's all in one logical function, you can group all your await
in one try and then take action based on error.
let a
let b
try {
a = await doAbc();
b = await do123(a);
} catch (err) {
// take appropriate action based on err
}
Create to function first return a and second using first function returned value to create b , somthing like this code
function firstCode(){
try {
a = await doAbc();
} catch(e) {
a = await doZxc();
}
return a;
}
function secondFunction(){
try {
b = await do123(firstFunction());
} catch (e) {
console.log(e);
return;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.