是否可以在iced coffee script
中创建异步构造函数:
class Animal
constructor: (autocb) ->
#some async action here
并称之为:
await new Animal, defer(animal)
当我尝试这样做时,得到了错误:
unexpected ,
答案 0 :(得分:2)
在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 ,
时只删除逗号。就这么简单。