4 ms·
Good but can you yield from the global scope? if not you still need to write at least a function. The hardest stuff in JS is that : function F(){throw "er
by camus2 13y ago
Good but can you yield from the global scope? if not you still need to write at least a function.
The hardest stuff in JS is that :
function F(){throw "error"}
try{setTimeout(F,1000)}catch(error){ console.log("error")}
we cant catch async errors outside the async callback , will generators fix this ? ( by the way , that's why you should never throw any error yourself in js ).
- coderzach 13y agoThat's why node modules generally don't throw exceptions, but rather pass them as the first argument to the callback. I think you can catch async errors if you call the throw method on the generator. http://wiki.ecmascript.org/doku.php?id=harmony:generators#methodthrow http://wiki.ecmascript.org/doku.php?id=harmony:generators#me...
- domenicd 13y agoYou cannot yield from the global scope; you have to explicitly be inside a generator function. This isn't really a problem in practice except for one-off-scripts, since most modules don't have side effects but instead export functions (possibly generator functions). But for those one-off scripts it is indeed a bit awkward... you could imagine ways to get around it. You can catch async errors with generators and promises; see e.g. the linked post, or http://jlongster.com/A-Study-on-Solving-Callbacks-with-JavaScript-Generators http://jlongster.com/A-Study-on-Solving-Callbacks-with-JavaS...
- masklinn 13y ago> we cant catch async errors outside the async callback , will generators fix this ? You can inject an exception into a generator via the throw method[0], and it'll be raised at the yield, as if `yield` had thrown[1]. JS exception handling facilities are still crap, though. [0] http://wiki.ecmascript.org/doku.php?id=harmony:generators#methodthrow http://wiki.ecmascript.org/doku.php?id=harmony:generators#me... [1] http://wiki.ecmascript.org/doku.php?id=harmony:generators#internal_methodthrow http://wiki.ecmascript.org/doku.php?id=harmony:generators#in...