11 ms·
Generators are the best, though I'm starting to worry I'm using them too much. edit: I'm using koa, so co is working in the background here. Should have said t
by ulisesrmzroche 13y ago
Generators are the best, though I'm starting to worry I'm using them too much.
edit: I'm using koa, so co is working in the background here. Should have said that earlier, my bad.
var save = function*(){
try {
yield db.insertUser();
} catch (e) {
throw e;
}
}
- alexgorbatchev 13y agohave you tried Co (https://github.com/visionmedia/co https://github.com/visionmedia/co) and if so what's your experience?
- bajtos 13y agoThat's not entirely true. If "db.insertUser()" is opening a database connection, who is going to close it on error? The idea behind Zones for Node.js is to auto-attach new resources to the current zone, so that they can be cleaned when the zone exits. AFAIK there is no solution for that at the moment.
- ulisesrmzroche 13y agoI'm thinking whatever db plugin you're using, but also this pseudo code actually has a lot of implicit things going in the background (koa/co, thunks, etc). It makes for some readable app code though.
- eldude 13y agoA major limitation of using try/catch is that V8 will DEOPT your function call, which can be a 5-10x performance hit (to the current closure). The best way to avoid this is with a silly hack: function trycatchit(fn, that, args) { try { return fn.apply(that, args) } catch(e) { return e } } var value = trycatchit(fn, null, 'someValue') if (value instanceof Error) { ... } Really though, you should just let co or whatever generator library you're using catch the error and rethrow it (or pass it to a callback) or be mindful to minimize your error handling. [1] https://github.com/CrabDude/trycatch/blob/master/lib/trycatch.js#L167-L211 https://github.com/CrabDude/trycatch/blob/master/lib/trycatc...