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.
How do I deal with exception handling in generator code in JavaScript?
A good solution would allow me to keep stack traces.