Skip to content

Instantly share code, notes, and snippets.

@jaydson
Last active November 27, 2019 02:02
Show Gist options
  • Save jaydson/7b3a91acf992f069954c to your computer and use it in GitHub Desktop.
Save jaydson/7b3a91acf992f069954c to your computer and use it in GitHub Desktop.
Asynchronous stat dir files using ES7 async/await + promisify + 6to5
// regeneratorRuntime polyfill is necessary
require('grunt-6to5/node_modules/6to5/polyfill');
// promisify-node is necessary to turn node callback functions in Promises
let promisify = require("promisify-node");
let fs = promisify('fs');
let path = require('path');
let dir = '/home/jaydson.gomes/public_html';
async function statDir() {
// Asynchronous node fs.readdir
let list = await fs.readdir(dir);
let filesInfo = [];
for (let i = 0; i < list.length; i += 1) {
// Asynchronous node fs.stat
let info = await fs.stat(path.join(dir, list[i]));
filesInfo.push(Promise.resolve(info));
}
return Promise.all(filesInfo);
}
(async function() {
let info = await statDir();
console.log(info);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment