RE: [Q] CoffeeScript styling question

184 views
Skip to first unread message

Domenic Denicola

unread,
Nov 17, 2012, 9:31:21 AM11/17/12
to q-con...@googlegroups.com
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.

Wout Mertens

unread,
Nov 17, 2012, 2:16:42 PM11/17/12
to q-con...@googlegroups.com
Excellent info! Using this style from now on :)

I like the catch() too - even more descriptive than fail().

I'm also noticing the => vs ->, I guess I'm not using object oriented programming enough :-)

Thanks!

Wout.

Asaf Shakarzy

unread,
Jul 9, 2013, 9:16:46 AM7/9/13
to q-con...@googlegroups.com
For more details, look at my post about Q & Coffee:

Reply all
Reply to author
Forward
0 new messages