The documentation states the following on try catch
statements:
The only exception is
try
, which doesn't catch exceptions when called from event handlers the main loop, for the same reason hand-rolled asynchronous code andtry
do not work well together.
I suspect that since db.find
is called asynchronously, the try
construct remains with the db.find
thread and does not stay in the main thread. This would result in the findings you describe.
One crude solution is to catch both function calls. However, I think the proper way to use await
is with the defer
function. Check out the documentation for an explanation.
Also, you might find the following helpful:
Possible Solutions
Place a try catch around both statements.
try await db.find "79", defer c, d catch error console.log "error catched" console.log error try a.fake() catch error console.log "error catched" console.log error
As described in the link above, place
db.find
outside the try catch and detect it's error another way.await db.find "79", defer err, id if err then return cb err, null try a.fake() catch error console.log "error catched" console.log error