Still not entirely sure what the big win with async/await is. Is it just a readability thing? Is there one "killer" case that demonstrates its superiority over callbacks?
There's a lot of benefit in being able to reuse the imperative language structures that people know and "love". I think it is more a writability thing.
I've been working to teach Promise thinking to another developer. Promises (and callbacks) are "easy" from a functional programming background, but for someone without much of a functional programming background like my colleague, it's certainly confusing. There is a rigor needed in writing Promises (and callbacks) in knowing what is in each closure and making sure that the return values of individual callbacks are in the "right shape" for the next callback in the chain.
Loops with Promises involve confusing things like higher level combinators like Promise.all and Promise.race, and that requires more new sorts of reasoning about return types than a "traditional" `for (let thing of list) { let result = await doThe(thing); /* do something with result */ }` loop.
Not that a need for things like Promise.all and Promise.race goes away with async/await but that it moves from being a must need to learn on the first pass of writing an algorithm out to being a performance optimization in advanced scenarios that can be done easier by someone more senior and/or in a later pass of code writing (such as a code or performance review).
It makes the learning curve to "doing asynchronous code right" a lot less sharp, smoothing out some of the complexity, and that can be a huge win for any team with a mixture of developers of different skill levels and skill sets.
I agree to some degree but at the end of the day it is a programming style.
I personally prefer the functional approach with bound methods to avoid callback hell, improve readability and ensure there is minimal amount of memory leaking.
That being said, async/await makes callback programming easier so it is another tool which may come handy in the situations where you really need it.
Comments
Still not entirely sure what the big win with async/await is. Is it just a readability thing? Is there one "killer" case that demonstrates its superiority over callbacks?
There's a lot of benefit in being able to reuse the imperative language structures that people know and "love". I think it is more a writability thing.
I've been working to teach Promise thinking to another developer. Promises (and callbacks) are "easy" from a functional programming background, but for someone without much of a functional programming background like my colleague, it's certainly confusing. There is a rigor needed in writing Promises (and callbacks) in knowing what is in each closure and making sure that the return values of individual callbacks are in the "right shape" for the next callback in the chain.
Loops with Promises involve confusing things like higher level combinators like Promise.all and Promise.race, and that requires more new sorts of reasoning about return types than a "traditional" `for (let thing of list) { let result = await doThe(thing); /* do something with result */ }` loop.
Not that a need for things like Promise.all and Promise.race goes away with async/await but that it moves from being a must need to learn on the first pass of writing an algorithm out to being a performance optimization in advanced scenarios that can be done easier by someone more senior and/or in a later pass of code writing (such as a code or performance review).
It makes the learning curve to "doing asynchronous code right" a lot less sharp, smoothing out some of the complexity, and that can be a huge win for any team with a mixture of developers of different skill levels and skill sets.
I agree to some degree but at the end of the day it is a programming style.
I personally prefer the functional approach with bound methods to avoid callback hell, improve readability and ensure there is minimal amount of memory leaking.
That being said, async/await makes callback programming easier so it is another tool which may come handy in the situations where you really need it.
Yes, you can match async/wait with generators thus using them for monad computations in Javascript.
Think of async as return and await as bind.
"you can match async/wait with generators thus using them for monad computations in Javascript"
I hear the words you are saying, I just cant visualise the concept. Could you ELI5, or post an example?
Yes, check Bodil Stokke's presentation, The Miracle of Generators, at GOTO 2016.
https://www.youtube.com/watch?v=6mCkLZ0cwAI
OK- so basically: async/await, when used with generators and promises, allows monads.
It's basically a fancy (but more limited) syntax for promise, with the same advantages.
Looks like awful readability to me. No idea what they're trying to solve here.