4 ms·
If we are on the subject of promises, Javascript is a poor language to learn about such things, no wonder developers understand the concept poorly. The thing t
by bad_user 14y ago
If we are on the subject of promises, Javascript is a poor language to learn about such things, no wonder developers understand the concept poorly.
The thing to understand about a promise is that monadic operations apply, which makes these instances composable and in Scala at least you can do all kinds of crazy things, like picking the first result that succeeded out of a list of promises, or filter/map over such promises, operations which are also async, producing other future results, or merging a bunch of future results and passing the merged-result to another unit of computation that executes in parallel and so on.
For Scala, there's an awesome Future/Promise implementation in Akka, soon to be merged in Scala 2.10. You can watch this presentation by Victor Klang on it: http://skillsmatter.com/podcast/scala/the-future-i-was-promised http://skillsmatter.com/podcast/scala/the-future-i-was-promi...
Note that in Scala, promises are actually named "Future", while a Promise is a Future that you build (and complete) yourself (or put it another way, a Future is an object from which you read, many times, while a Promise is an object to which you write once). A Promise implements the Future interface.
And because Scala provides syntactic sugar for monadic operations, you can write your code like this ...
val googleResult = queryGoogleMaps(lat, lon)
val bingResult = queryBingMaps(lat, lon)
val yahooResult = queryYahooMaps(lat, lon)
val bestLocation = for {
location1 <- googleResult
location2 <- bingResult
location3 <- yahooResult
} yield compareAndPickBestResult(location1, location2, location3)
Notice how this code could be completely synchronous, and yet if those functions I used return some Future[Result], then it still works as intended, with "bestLocation" being another Future result, so if you need it right now, you'll have to wait for it ...
Async.wait(bestLocation, 3 seconds)
- recursive 14y ago> The thing to understand about a promise is that monadic operations apply If that's necessary to understand, then javascript isn't the problem. Most developers do not understand monadic operations.
- bad_user 14y agoThat's actually a pity, because monads are easy to understand, being just like a normal design pattern ... name your favorite one and it's probably harder to understand than monads, because monads are actually well defined. I was also giving Scala as a better language to experiment with such things, because the funny thing about Scala is that monads are more composable in comparison to Haskell or other functional languages.
- recursive 14y agoI've spent more time trying to understand monads than all the named design patterns put together. And I don't have a favorite design pattern, just a vague skepticism for the lot of them, and a strong skepticism for anyone who promotes them. Anyway, being well-defined is not a sufficient condition for being easy to understand.