Skip to content

Instantly share code, notes, and snippets.

@wavded
Last active December 20, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wavded/6076200 to your computer and use it in GitHub Desktop.
Save wavded/6076200 to your computer and use it in GitHub Desktop.
var fs = require('fs')
var Q = require('q')
var fs_stat = Q.denodeify(fs.stat)
var files = ['./fixtures/file1', './fixtures/file2', './fixtures/file3']
function getStatsSeries (files) {
var d = Q.defer()
var results = []
files.reduce(function (last, file) {
return last.then(function (stat) { results.push(stat); return fs_stat(file) })
}, Q())
.then(function (lastStat) {
results.shift() // first result is the initialVal for the reduce (garbage)
results.push(lastStat) // last result happens in this promise
d.resolve(results)
}, d.reject)
return d.promise
}
getStatsSeries(files).then(console.log, console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment