I have an async function that I expect to throw exception on failure. However something seems to preventing this:
by omitting the try catch blocks I expect an exception to be thrown which I want to handle outside of the function.
The actual result I get is somewhat confusing:
(node:10636) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): E11000 duplicate key error index.
(node:10636) 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.
async f(obj) {
await db.collection('...').save(obj);
}
I get the same result when I try to catch the exception and throw something else instead:
async f(obj) {
try {
await db.collection('...').save(obj);
} catch(e) {
throw e.message;
}
}
The function is called from a try block, so don't see how this is an Unhandled Promise.
I am trying to use f
as a parameter of another function:
g(obj, handler) {
try {
handler(obj);
} catch(e);
...
}
}
g(objToSave, f);