foreachasync.js/examples/node/foreachasync-fs-readdir.js

23 lines
581 B
JavaScript
Raw Normal View History

2014-01-22 20:37:28 +00:00
'use strict';
2015-01-03 01:08:02 +00:00
var PromiseA = require('bluebird')
, fs = PromiseA.promisifyAll(require('fs'))
2014-01-22 20:37:28 +00:00
, forEachAsync = require('foreachasync').forEachAsync
, path = require('path')
, dirpath = path.join(__dirname, 'testfiles')
;
fs.readdir(dirpath, function (err, nodes) {
2015-01-03 01:08:02 +00:00
forEachAsync(nodes, function (node) {
2014-01-22 20:37:28 +00:00
var filepath = path.join(dirpath, node)
;
console.log(filepath);
2015-01-03 01:08:02 +00:00
return fs.readFileAsync(filepath, null).then(function (contents) {
2014-01-22 20:37:28 +00:00
console.log(node, contents.length);
});
}).then(function () {
console.log('All Done!');
});
});