Hendrik
Hendrik

Reputation: 4939

Coffeescript promises on multiple lines syntax

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

Answers (4)

mu is too short
mu is too short

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

Exinferis
Exinferis

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

deceze
deceze

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

Thilo
Thilo

Reputation: 262834

If you are willing to put up with semicolons:

 @update()
   .success (company)=>;
   .error (company)=>;
   .finally (company)=>;

Upvotes: 2

Related Questions