Simplified code:
// File A
module.exports = new Promise((res, rej) => {
... do stuff
if (someErr) {
return rej(someErr);
}
return res(stuff)
}
// File B
const fileA = require('./fileA');
... express stuff
app.get('/getA', (req, res) => {
fileA
.then(data => res.send(data))
.catch(e => res.send(404, e));
});
As you can see the promise rejection gets caught when running a post request to a route in file B
. This way it is also possible to send a 404 response with the original error trough.
The problem is, there is an Unhandled Promise Rejection from file A
whenever the route is called. VERY annoying.
(node:5040) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5040) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Yes, sure, i could catch the rejection in file A
, but i would rather prefer it propagate upstream and catch it later.