Skip to content

Commit 6c86e7d

Browse files
committed
4.2 - promisify excution
1 parent 56a63b0 commit 6c86e7d

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

04-asynchronous-control-flow-patterns-with-callbacks/listNestedFiles.mjs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readdir, writeFile } from 'fs';
2+
import { promisify } from 'util';
23

34
/**
45
* ### 로컬 파일시스템의 디렉터리경로를 입력으로 받으며 비동기적으로 반복하여 발견되는 모든 서브 디렉터리를 비동기적으로 반환.
@@ -61,18 +62,16 @@ function listNestedFiles(dir, cb) {
6162
//--------------------------------------------------------------------------------
6263

6364

64-
65-
listNestedFiles('../', (err, result) => {
66-
if (err) {
67-
console.error(err);
68-
return;
69-
}
70-
71-
writeFile('./listNestedFiles.json', JSON.stringify(result, null, 2), (err) => {
72-
if (err) {
73-
console.error(err);
74-
return;
75-
}
76-
console.log('done');
77-
});
65+
// 실행 하는 부분은 promisify 이용해보았다.
66+
promisify(listNestedFiles)('../')
67+
.then(result => {
68+
promisify(writeFile)(
69+
'./listNestedFiles.json',
70+
JSON.stringify(result, null, 2)
71+
).then(
72+
() => console.log('done'),
73+
err => console.error(err)
74+
);
75+
}).catch(err => {
76+
console.error(err);
7877
});

0 commit comments

Comments
 (0)