3 ms·
I agree with this list completely, but it doesn't mention one of the best reasons generators and promises are awesome: async/await-style asynchronous programmin
by tlrobinson 11y ago
I agree with this list completely, but it doesn't mention one of the best reasons generators and promises are awesome: async/await-style asynchronous programming eliminates callback hell by letting you write asynchronous code as if it were synchronous, including using control structures like conditionals, loops, and try/catch.
And you can do it in (most) browsers today using a transpiler like Babel and a library like Blurebird, Q, co, or task.js.
The rest of the things are nice, but promises/generators (and eventually async/await) are game-changers.
- ehaliewicz2 11y agoOf course, with proper tail calls, you can get generators and any kind of control structure almost for free :)
- amelius 11y agoThe problem with Javascript generators, however, is that you're not allowed to call other functions from them, which yield. This very much limits the kind of async stuff you can do with them.
- tlrobinson 11y agoNot really (at least not the kind of stuff I mentioned), those functions can just return promises which the caller yields: http://codepen.io/anon/pen/WvzGLa http://codepen.io/anon/pen/WvzGLa Granted async/await syntax will make this a bit nicer: async function funcA() { await sleep(1000); // ...
- amelius 11y agoBut then the called functions have to be written async style.