是否可以iced coffee script
制作异步构造函数:
class Animal
constructor: (autocb) ->
#some async action here
并称它为:
await new Animal, defer(animal)
当我尝试这样做时,出现错误:
unexpected ,
是否可以iced coffee script
制作异步构造函数:
class Animal
constructor: (autocb) ->
#some async action here
并称它为:
await new Animal, defer(animal)
当我尝试这样做时,出现错误:
unexpected ,
在 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 ,
删除逗号时。就这么简单。