-
Notifications
You must be signed in to change notification settings - Fork 0
/
async-error.js
63 lines (53 loc) · 1.19 KB
/
async-error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
const fs = require('fs');
try {
undefined[0];
} catch (e) {
console.error(e);
}
console.log('alma');
try {
setTimeout(() => {
// throw 'kecske';
}, 10);
} catch (e) {
console.error(e);
} finally {
console.log('finally');
}
fs.readFile('alma.txt', (err, content) => {
if (err) {
console.log('nincs alma');
return;
}
});
const filePromise = new Promise((resolve, reject) => {
fs.readFile('korte.txt', (err, content) => {
if (err) {
reject(err);
return;
}
resolve(content.toString());
});
});
fs.promises.readFile('filename.txt')
.then(content => content.toString().split('\n')[0])
.then(fileName => fs.promises.readFile(fileName))
.then(c => c.toString())
.then(c => console.log(c))
.catch((e) => console.error(e));
fs.promises.readFile('filename.txt')
.then(content => content.toString().split('\n')[0])
.then(fs.promises.readFile)
.then(c => c.toString())
.then(console.log)
.catch((e) => console.error(e));
const myFileReader = (filename) => {
return fs.promises.readFile(filename)
.then(c => c.toString());
};
Promise.all([
myFileReader('filename.txt'),
myFileReader('korte2.txt'),
])
.then(console.log);