Last active
December 20, 2015 05:09
-
-
Save wavded/6076200 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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