Let's assume I have this pseudocode inside a routes.js file:
var pkg = require('random-package');
app.post('/aroute', function(req, res) {
pkg.impl_func(data, function (err, result) {
myFunction(entity).then(user=>{
//DO_STUFF_HERE
res.render('page.ejs');
}).catch(err => {
console.log(err);
res.render('error.ejs');
});
});
});
function myFunction(username) {
//.....
}
The pkg I used is one found on the npmjs website. myFunction() is always my function.
In my code you can see that i have implemented then/catch statement for when myFunction() fails.
So when that happens error.ejs is rendered.
But what happens when the npm package fails?
In the terminal i get the error message but there is no error handling on the server side.
This means, when it fails the user will not be notified with error.ejs, it is obvious since this functionality is omitted from my code.
But what are the ways to render error.ejs, when pkg fails?
Since I am already using .then()/.catch() technique below, can I also do it above?
In other words, can I nest .then()/.catch() statements?
Can I surround the outer code to a try/catch (while still having a try/catch inside)?