Q:

How to loop chained calls elegantly in JavaScript/CoffeeScript?

I'm using Soda to write Selenium tests in Node.js and I have a situation where I have to press the down key several times.
The code currently looks like this:                                             
browser                                                                     
  .chain                                                                    
  .setSpeed(200)                                                            
  .session()                                                                
  .open('/')                                                                
  .click("id=save")                                                         
  .focus(editor)                                                            
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  
  .keyDown(editor, '\\40')                                                  

  ...                                                                       

How could I DRY this up?                                                        
Just using a loop like this does not work with this lib:                        
var b = browser.chain()                                                     
for (var i = 0; i < 10; i++) {                                              
  b.keyDown(editor, '\\40')                                                 
}                                                                           

Awesome ideas?                                                                  
I could use the async API in Soda and for example async-lib to help me out, but that's not what I'm asking here. It makes some other things ugly.

A:

There is a method called and for doing complicated things in the middle of a command chain:
browser
  .chain
  .setSpeed(200)
  .session()
  .open('/')
  .click("id=save")
  .focus(editor)
  .and(function (browser) {
    for (var i = 0; i < 10; i++) {
      browser.keyDown(editor, '\\40')
    }
  })
  ...

See the README for more information: https://github.com/learnboost/soda

A:

Did you try replacing the b variable in your loop?
var b = browser.chain()                                                     
for (var i = 0; i < 10; i++) {                                              
  b = b.keyDown(editor, '\\40')                                                 
}                                                                           

