Pass a list of named functions into function in co

2019-09-01 05:02发布

问题:

In the API Documentation there is a snippet:

casper.waitFor(function check() {
    return this.evaluate(function() {
        return document.querySelectorAll('ul.your-list li').length > 2;
    });
}, function then() {
    this.captureSelector('yoursitelist.png', 'ul.your-list');
}, function timeout() {
    this.echo("I can't haz my screenshot.").exit();
});

I need this, but in a coffeescript project. I tried to rewrite it into coffeescript but it didn’t work. Even if i let js2coffe do the job, i get some invalid coffeescript from valid javascript:

i dont know how to pass a list of named functions into another function correctly.

回答1:

CoffeeScript doesn't really support named functions like that, see:

  • How do I create a named function expressions in CoffeeScript?
  • Function declaration in CoffeeScript

That specific example doesn't need them anyway, it looks like they're just there for documentation purposes so you could write:

check    = -> @evaluate(-> document.querySelectorAll('ul.your-list li').length > 2)
and_then = -> @captureSelector('yoursitelist.png', 'ul.your-list')
timeout  = -> @echo("I can't haz my screenshot.").exit()
casper.waitFor(check, and_then, timeout)

in CoffeeScript to get the same effect.