Reputation: 4939
I have this code in Coffeescript:
@update().success((company)=>
).error((company)=>
).finally((company)=>
)
I wonder if this can be changed to remove some of the brackets. Something like this:
@update()
.success (company)=>
.error (company)=>
.finally (company)=>
But I always get an SyntaxError: [stdin]:18:9: unexpected .
. Any idea what I am doing wrong?
Thanks.
Upvotes: 1
Views: 85
Reputation: 434945
Just because you can use anonymous functions doesn't mean you have to. Busting the functions out and giving them names often makes your code clearer, easier to read, and less whitespace-fragile.
For example:
frobnicate_the_things = => # some pile of logic goes here
complain_about_the_problems = => # and another pile here
clean_up_the_mess = => # and yet another here
@update()
.success frobnicate_the_things
.error complain_about_the_problems
.finally clean_up_the_mess
Of course I don't know what your callbacks are actually doing so I had to make up some silly names but that's not the point. The point is that you have nice self-documenting code without a bunch of fiddling around with syntax and whitespace.
Upvotes: 3
Reputation: 687
You're almost there, you just forgot to add return
statement:
@update()
.success (company)=>
return
.error (company)=>
return
.finally (company)=>
return
Upvotes: 0
Reputation: 522597
As long as you fill in a function body, that syntax works just fine:
@update()
.success (company) => true
.error (company) => true
.finally (company) => true
Otherwise you'd have to clearly delineate the callback functions:
@update()
.success ((company) =>)
.error ((company) =>)
.finally ((company) =>)
But then again, you wouldn't be writing an empty callback in the first place.
Upvotes: 1
Reputation: 262834
If you are willing to put up with semicolons:
@update()
.success (company)=>;
.error (company)=>;
.finally (company)=>;
Upvotes: 2