2

I'm trying to do some work with Promises in Coffeescript, however, I'm getting a syntax error that I can't figure out. It doesn't seem to accept that the then method takes two arguments.

Example:

slowTask = (num) ->
  new Promise((resolve, reject) ->
    if (num == 1)
      resolve(num)
    else
      reject(num)
  )

slowTask(1).then((data) -> console.log("foo"), () -> console.log("bar"))

Seems like it should work, but I'm instead getting a failure:

Error on line 9: unexpected , 

Admittedly, I'm only a few hours into coffeescript, so there may be a fundamental misunderstanding of its syntax, but as far as I can tell, it looks like the code should compile fine.

What am I missing?

user3308774
  • 1,354
  • 4
  • 16
  • 20

2 Answers2

2

There is no way for CoffeeScript to understand what that comma is doing.

You need to put parenthesis around your entire function, if you're going to have multiple function statements on the same line:

slowTask(1).then ((data) -> console.log("foo")), (() -> console.log("bar"))
user229044
  • 232,980
  • 40
  • 330
  • 338
1

In addition to meagar's comment, you can use comma if you break your code in few lines, like so:

slowTask(1).then (data) ->
  console.log "foo"
, ->
   console.log "bar"
SET001
  • 11,480
  • 6
  • 52
  • 76