Proper syntax for array.forEach() in coffeescript

coffeescript, node.js, syntax

Solution

You're defining a function in a loop. A common pitfall in JS. You'll need to close over the iterator.

`for file in list then do (file) =>`

Problem

I have a following code in coffeescript to recursively list directories (converted from javascript): ``` fs = require 'fs' walk = (dir, done) -> results = [] fs.readdir dir, (err, list) -> return done(err) if err pending = list.length return done(null, results) unless pending list.forEach (file) -> file = "#{dir}\\#{file}" fs.stat file, (err, stat) -> if stat and stat.isDirectory() walk file, (err, res) -> results = results.concat(res) done null, results unless --pending else results.push file done null, results unless --pending walk __dirname, (err, results) -> throw err if err console.log results ``` As you can see I am using `list.forEach (file) ->` and it's working. But when I try to replace it with `for file in list` then it doesn't work properly. What do I do wrong?

Original source