【问题标题】:await defer constructor, async constructorawait 延迟构造函数,异步构造函数
【发布时间】:2013-12-27 09:42:17
【问题描述】:

是否可以在iced coffee script 中制作异步构造函数:

class Animal
  constructor: (autocb) ->
    #some async action here

然后这样称呼它:

await new Animal, defer(animal)

当我尝试这样做时,出现错误:

unexpected ,

【问题讨论】:

    标签: javascript coffeescript iced-coffeescript


    【解决方案1】:

    在 CoffeeScript 中,逗号用作参数的分隔符。例如:

    add 2, 3
    

    您可以选择在参数周围加上括号以使其更明确:

    add(2, 3)
    

    但是你不能在函数和参数之间加逗号:

    add, 2, 3   # not allowed
    add(, 2, 3) # can you see your mistake?
    

    构造函数也是如此:

    new Animal defer(animal)  # this is ok
    new Animal(defer(animal)) # defer(animal) is just an argument
    

    但是你不能在new Animal 和第一个参数之间加逗号:

    new Animal, defer(animal)   # not allowed
    new Animal(, defer(animal)) # can you see your mistake?
    

    await 也是如此:

    await new Animal defer(animal)  # this is ok
    await new Animal(defer(animal)) # again defer(animal) is just an argument
    

    但是你不能在函数和第一个参数之间加逗号:

    await new Animal, defer(animal)   # not allowed
    await new Animal(, defer(animal)) # can you see your mistake?
    

    所以回答你的问题:是的,可以在冰咖啡脚本中创建一个异步构造函数。与所有异步函数一样,最后一个参数必须始终是由defer 生成的回调函数。

    下次当编译器说unexpected , 时,只需删除逗号即可。就这么简单。

    【讨论】:

      猜你喜欢
      • 2012-08-05
      • 1970-01-01
      • 2014-10-28
      • 2018-06-26
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 2011-12-30
      • 2017-09-11
      相关资源
      最近更新 更多