【发布时间】:2014-09-14 15:20:04
【问题描述】:
我有以下异步代码。
for fileName in sourceFiles
console.log 'dealing with ', fileName
async.waterfall [
(callback) ->
console.log "going to read ", fileName
fs.readFile fileName, (err, content) ->
if err then throw err
callback null, fileName, content
return
(fileName, content, callback) ->
console.log 'length of: ', fileName, ' is: ', content.length
我预计输出会是这样的:
dealing with file1
going to read file1
length of file1 is 10
dealing with file2
going to read file2
length of file 2 is 20
相反,我得到的是:
dealing with file1
dealing with file2
going to read file2
going to read file2 <- note it is the same file repeated
length of file2 is 20
length of file2 is 20
我无法弄清楚为什么会这样。 (这是一个coffeescript 没问题。在JS 中也是一样的输出)
【问题讨论】:
标签: javascript for-loop asynchronous coffeescript async.js