0

如何在iced coffee script使用returnand中返回几个值autocb

没有autocb我可以做到:

func = (cb)=>
   cb returnVal1, returnVal2

如何使用autocb?这段代码...

func = (autocb)=>
   return returnVal1, returnVal2 

...抛出错误:

SyntaxError: unexpected ,
4

2 回答 2

1

您收到错误是因为您在 JavaScript 中不能返回多个值。您可以将这两个值包装在一个数组中并在调用后对其进行解构...

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

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

...但您可能应该只使用您的第一个示例。autocb是简单的语法糖(一个参数而不是一行),并且完全不需要使用 IcedCoffeeScript。

于 2014-01-04T21:18:50.670 回答
0

解构将按照此处所述进行: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}"
于 2015-01-29T02:21:03.387 回答