Search code examples
javascriptnode.jscoffeescriptzombie.js

doing a while loop in node.js, always give me the last array item?


im using the zombie.js library , to do some testing for the dom, i have this script

Browser = require "zombie"
arr = new Array("http://yahoo.com", "http://google.com", "http://msn.com")
i = 0
while i < arr.length
    b = new Browser()
    b.visit arr[i], ->
        console.log b.text "title"
    b.close()
    i++

everything works, but it only logs the msn title two times, so the while loop is logging the last item in the array twice. i can't seem to see what the problem is?


Solution

  • You're registering a callback for when the browser's visit has completed. By that time, the loop has run to completion, so the variable b points to the last browser you created, which means its title will be that of MSN, not those of the other pages. In order to fix this, use a closure:

    Browser = require "zombie"
    arr = new Array("http://yahoo.com", "http://google.com", "http://msn.com")
    i = 0
    createBrowser = (url) ->
        b = new Browser()
        b.visit url, -> console.log b.text "title"
        b.close()
    
    while i < arr.length
        createBrowser(arr[i++])
    

    This works because now there is a separate scope for each browser you create, and a separate browser variable.

    Alternatively, use the callback arguments for ZombieJS's visit function:

    Browser = require "zombie"
    arr = new Array("http://yahoo.com", "http://google.com", "http://msn.com")
    i = 0
    while i < arr.length
        b = new Browser()
        b.visit arr[i], (e, myBrowser) ->
            console.log myBrowser.text "title"
        b.close()
        i++