3 ms·
Asynchronicity is great, but javascript doesn't provide the tools to do it properly. > (...) try writing a function call that requires information from two sep
by pkinsky 13y ago
Asynchronicity is great, but javascript doesn't provide the tools to do it properly.
> (...) try writing a function call that requires information from two separate HTTP API responses; I basically need to draw a diagram of what happens with async.waterfall for a task that, given synchronicity, would've been solved with a trivial three-liner.
It's a lot easier to handle asynchronicity in strongly-typed languages with monadic for comprehensions. In Scala, for example:
val res: Future[Foobar] = for {
a <- makeHttpRequestA()
b <- makeHttpRequestBFromA(a)
} yield new Foobar(a, b)
You can then register callback functions which operate on either a Success[Foobar] or a Failure[Throwable].
Synchronous code isn't simpler, it just hides complexity like caltrops in tall grass.
(slightly modified version of my post on Github)
- eldude 13y agoco(function *(){ let promiseA = makeHttpRequestA() let promiseB = makeHttpRequestBFromA(yield promiseA) var res = new Foobar(yield promiseA, yield promiseB); })();
- densh 13y agowith scala.async [1]: val res = async { val a = await(makeHttpRequestA()) val b = await(makeHttpRequestBFromA(a)) new Foobar(a, b) } [1] https://github.com/scala/async https://github.com/scala/async