Skip to content

Nested promise #65

@LinaYahya

Description

@LinaYahya

.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions