Skip to content

Instantly share code, notes, and snippets.

@isaacs
Forked from Ommadawn/gist:429792
Created June 8, 2010 09:12
Show Gist options
  • Select an option

  • Save isaacs/429797 to your computer and use it in GitHub Desktop.

Select an option

Save isaacs/429797 to your computer and use it in GitHub Desktop.
function readAllHelps (cb) {
var help_text = []
, didError = false
function error (er) {
if (didError) return undefined
didError = true
cb(er)
}
fs.readdir('help', function(er, files) {
if (err) return error(er)
var len = files.length
files.forEach(function (file) {
fs.readFile('help/'+file, function(er, data) {
if (didError) return undefined
if(er) return error(er)
help_text[file] = data
if (0 === --len) cb(null, help_text)
})
})
})
}
@Ommadawn

Ommadawn commented Jun 8, 2010

Copy link
Copy Markdown

Thanks so much, you're a great help !

@Ommadawn

Ommadawn commented Jun 8, 2010

Copy link
Copy Markdown

I think I now get how to structure my code thanks to your example.

I would never have come up with the counter decrementation to know when to call the callback... Very clever, I'll remember that trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment