0

How to return couple values in iced coffee script using return and autocb?

Without autocb I can do:

func = (cb)=>
   cb returnVal1, returnVal2

How to implement this using autocb? This code ...

func = (autocb)=>
   return returnVal1, returnVal2 

... throws error:

SyntaxError: unexpected ,
Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166

2 Answers2

1

You are getting an error because you can't return more than one value in JavaScript. You could wrap the two values in an array and destructure it after calling...

func = (autocb)=>
   return [returnVal1, returnVal2] 

await func defer(returnVals)
[returnVal1, returnVal2] = returnVals

...but you should probably just use your first example. autocb is simple syntactical sugar (one argument instead of one line), and not at all necessary to using IcedCoffeeScript.

Dylan
  • 144
  • 1
  • 7
0

Destructuring will work as mentioned here: https://github.com/maxtaco/coffee-script/issues/29

func = (thing, autocb) ->
   thing1 = doSomething(thing)
   thing2 = doSomethingElse(thing)

   {thing1, thing2}


await funct thing, defer {thing1, thing2}

console.log "#{thing1} and #{thing2}"
S.E.T.
  • 137
  • 1
  • 8
  • `ICED warning: overused deferral at ` on line `autocb thing1, thing2` - is because of autocb returns automatically, so in fact your return 2 times – Maxim Yefremov Jan 29 '15 at 02:58
  • You're absolutely right. The JS that gets generated from what I was suggesting is autocb(autocb(thing1, thing2)). The answer is destructuring as I found here: https://github.com/maxtaco/coffee-script/issues/29 – S.E.T. Jan 29 '15 at 22:09