Skip to content

Instantly share code, notes, and snippets.

@helton
Created July 20, 2017 02:47
Show Gist options
  • Save helton/876f7e97c7ee61cb1f6ece5cd54ed1bc to your computer and use it in GitHub Desktop.
Save helton/876f7e97c7ee61cb1f6ece5cd54ed1bc to your computer and use it in GitHub Desktop.
Full Stack Academy - Aula 01 - Exercicio 05
/*
* [Exercício 5 - extra]
*
* Dado a lista de arquivos/diretórios retornada no exercício anterior,
* mostre quais são arquivos.
* (utilize fs.stat(caminho, (err, stat) => stat.isFile()) para isso.)
*/
const fs = require('fs')
const readdirPromise = (path) => new Promise((resolve, reject) =>
fs.readdir(path, (err, files) =>
err ? reject(err) : resolve(files)
)
)
const readFileStats = (path) => new Promise((resolve, reject) =>
fs.stat(path, (err, stats) =>
err ? reject(err) : resolve({ path, isFile: stats.isFile() })
)
)
const showFilesOnly = async (path) => {
const paths = await readdirPromise(path)
const stats = await Promise.all(paths.map(readFileStats))
const filesOnly = stats.filter(stat => stat.isFile)
console.log(filesOnly);
}
showFilesOnly('./')
@tuliofaria
Copy link

Certinho Helton. Recomendo que de uma olhada nesse vídeo também: https://www.youtube.com/watch?v=nnsz91rhWuA

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