Last active
July 25, 2017 16:53
-
-
Save hanpama/dac30d4a83d9f53a75b622143c7fa70c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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