Promises and error handling

Promise objects can be in one of three states:

  • Pending: This is the initial state, neither fulfilled nor rejected
  • Fulfilled: This is the final state where it executed successfully and produced a result
  • Rejected: This is the final state where execution failed

Consider this code segment similar to the one we'll use later in this chapter:

notes.read(req.query.key) 
.then(note => { return filterNote(note); }) 
.then(note => { return swedishChefSpeak(note); }) 
.then(note => { 
    res.render('noteview', { 
        title: note ? note.title : "", 
        notekey: req.query.key, 
        note: note 
    }); 
}) 
.catch(err => { next(err); }); 

There are several places where errors can occur in this little bit of code. The notes.read function has several possible ...

Get Node.js Web Development - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.