Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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

Problem

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 
        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?

Problem courtesy of: unknown

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 

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 
        console.log myBrowser.text "title"
    b.close()
    i++
Solution courtesy of: Gijs

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

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

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×