Imagine we have a function like so:
async function doSomethingWithFriends() {
let user, friends, friendsOfFriends = null;
try {
user = await getUser();
} catch(err){
return [401, err]
}
try {
friends = await getFriends(user);
} catch (err){
return [500, err];
}
try {
friendsOfFriends = await getFriendsOfFriends(friends);
} catch(err){
return [500, err]
}
return doFriendsOfFriends(friendsOfFriends);
}
instead is there some established pattern to avoid this somewhat boilerplate code?
There are two problems with the above code:
- it's noisy
- we cannot use
const
One solution is to only use try-catch in the caller functions, but I am wondering if there is a way to solve it all the way down/up.
tryblock.