So recently generators kicked in in NodeJS and I'm able to do something like:
Promise.coroutine(function *(query){
var handle = yield db.connect(Settings.connectionString); //async, returns promise
var result = yield db.query(query); // async, returns promise
return result;
});
Now generators are awesome in that they let me do async/await in JS. I really like being able to do that.
However, one issue arises. Generators work with try/catch blocks, so let's say I have code that looks like this:
Promise.coroutine(function *(){
try{
var db = yield DBEngine.open("northwind"); // returns promise
var result = yield db.query("SELECT name FROM users"); // returns promise
return res;
} catch (e){
//code to handle exception in DB query
}
});
(note,Promise.coroutine is from bluebird)
Notice the bug? There is a reference error, however - the catch will swallow it.
When I put try/catch in 95% of cases what I want to catch is logical errors and I/O errors and not syntax or type errors. I want to be very very aware of those. Callbacks use an err
first parameter and in generators I'm not sure of what the substitute is.
A good solution would allow me to keep stack traces.
So basically this is not related to asynchronous system but rather to a particular error handling. Try something like this:
var global_error_check = function(e) {
if (e && e.name === "ReferenceError") {
throw e;
}
// put other error types here that you don't want to catch
}
try {
...
} catch(e) {
global_error_check(e);
// handle other errors here
}