I do
getPromise().then(
(val) => foo
(err) => bar
)
or
getPromise().then(
(val) =>
doStuff(val)
doMoreStuff(val)
(err) => bar
)
It's pretty rare to have both a fulfillment and rejection handler though; I usually leave my rejection handlers for very high up in the chain (just like catch blocks). Then you can do nice things like
getPromise().then (val) =>
doStuff(val)
doMoreStuff(val)
getPromiseAtEndOfChain().catch (err) =>
processErr(err)
[catch is a synonym for fail that CoffeeScript will make work cross-browser by translating it to `getPromiseAtEndOfChain()["catch"](...)`.]
From:
q-con...@googlegroups.com [mailto:
q-con...@googlegroups.com] On Behalf Of Wout Mertens
Sent: Saturday, November 17, 2012 09:09
To:
q-con...@googlegroups.com
Subject: [Q] CoffeeScript styling question
Hi all,
I'm pretty new to promises and totally new to Q.
I like how promises clean up the code, but I'm wondering about how to set up the then() promise callbacks in a pretty way in CoffeeScript.
Is there a canonical way?
I came up with
getPromise().then (val) ->
foo
, (err) ->
bar
which compiles to getPromise().then(function(val) {return foo;}, function(err) {return bar;});
or
getPromise()
.then (val) ->
foo
.fail (err) ->
bar
=> promise.then(function(val) {return foo;}).fail(function(err) {return bar;});
which is more readable imho because of the keywords but from my understanding ruins chaining?
I'd love to know what others came up with, also for other often used constructs.
Thanks!
Wout.