Writing Clean & Maintainable Code
Which approach to organizing asynchronous code is more maintainable?
// Option A:
getUser(id)
.then(user => {
return getOrders(user.id);
})
.then(orders => {
return processOrders(orders);
})
.catch(error => {
handleError(error);
});
// Option B:
async function processUserOrders(id) {
try {
const user = await getUser(id);
const orders = await getOrders(user.id);
return await processOrders(orders);
} catch (error) {
handleError(error);
}
}