Skip to content

Instantly share code, notes, and snippets.

@hanpama
Last active July 25, 2017 16:53
Show Gist options
  • Save hanpama/dac30d4a83d9f53a75b622143c7fa70c to your computer and use it in GitHub Desktop.
Save hanpama/dac30d4a83d9f53a75b622143c7fa70c to your computer and use it in GitHub Desktop.
/**
* NodeJS에서의 비동기 작업 예제
*/
const fs = require('fs');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const writeFile = util.promisify(fs.writeFile);
const readFile = util.promisify(fs.readFile);
const writeLSWithCallback = () => {
fs.readdir('.', (err, files) => {
fs.writeFile('ls.json', err => {
fs.readFile('ls.json', (err, data) => {
console.log(data.toString());
})
})
})
}
const writeLS = () => {
return readdir('.')
.then(result => writeFile('ls.json', JSON.stringify(result)))
.then(() => readFile('ls.json'))
.then(content => console.log(content.toString()))
}
const asyncWriteLS = async () => {
const result = await readdir('.')
await writeFile('ls.json', JSON.stringify(result));
const content = await readFile('ls.json');
console.log(content.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment