-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
Description
Blog-Post/server/controllers/getUsernameBasedOnId.js
Lines 7 to 15 in ce45315
| .then((ids) => { | |
| getUserNameUponIdQuery(userId) | |
| .then((data) => { | |
| const { name } = data.rows[0]; | |
| res.json({ name, curUserId: ids.rows[0].id }); | |
| }) | |
| .catch(() => res.status(500).json({ message: 'Internal Server Error' })); | |
| }) | |
| .catch(() => res.status(500).json({ message: 'Internal Server Error' })); |
As .then() return promise we can simply chain promises,
.then((ids) => {
return getUserNameUponIdQuery(userId);
})
.then((data) => {
const { name } = data.rows[0];
res.json({ name, curUserId: ids.rows[0].id });
})
.catch(() => res.status(500).json({ message: 'Internal Server Error' })); And we can notice that every throw error will end up in the final catch block, So no need to have two catch blocks here.
highly recommend reading more about promise chaining and how to avoid nested promise.
Reactions are currently unavailable