简体   繁体   中英

await defer constructor, async constructor

Is it possible in iced coffee script to make async constructor:

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

And call it like:

await new Animal, defer(animal)

When I try to do it, got error:

unexpected ,

In CoffeeScript commas are used as separators for arguments. For example:

add 2, 3

Optionally you may put parentheses around arguments to make it more explicit:

add(2, 3)

However you may not put a comma between the function and the arguments:

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

The same goes for constructor functions:

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

However you can't put a comma between new Animal and the first argument:

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

The same goes for await :

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

However you can't put a comma between the function and the first argument:

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

So to answer your question: yes, it is possible to make an async constructor in iced coffee script. Like all asynchronous functions the last argument must always be the callback function generated by defer .

Next time when the compiler says unexpected , just remove the comma. It's as simple as that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM