【问题标题】:return several values with autocb in iced coffee script在冰咖啡脚本中使用 autocb 返回几个值
【发布时间】:2014-01-20 00:39:52
【问题描述】:

如何使用returnautocbiced coffee script 中返回一对值?

没有autocb我可以:

func = (cb)=>
   cb returnVal1, returnVal2

如何使用autocb 实现这一点?这段代码...

func = (autocb)=>
   return returnVal1, returnVal2 

...抛出错误:

SyntaxError: unexpected ,

【问题讨论】:

    标签: iced-coffeescript


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      解构将按照此处所述进行: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}"
      

      【讨论】:

      • ICED warning: overused deferral at <anonymous>在线autocb thing1, thing2 - 是因为autocb自动返回,所以实际上你返回2次
      • 你说得对。根据我的建议生成的 JS 是 autocb(autocb(thing1, thing2))。正如我在这里找到的那​​样,答案是解构:github.com/maxtaco/coffee-script/issues/29
      猜你喜欢
      • 2012-05-04
      • 2015-11-06
      • 2013-09-12
      • 2012-09-03
      • 2014-01-10
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多