-
Notifications
You must be signed in to change notification settings - Fork 1
/
catch.js
55 lines (49 loc) · 1.18 KB
/
catch.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
const throwsSync = () => { throw new Error("thrown in throwsSync") }
const throwsAsync = async () => { throw new Error("thrown in throwsAsync") }
const logsSyncThrowsAsync = async () => {
console.log("log in logsSyncThrowsAsync")
throw new Error("thrown in logsSyncThrowsAsync")
}
const logsAsyncThrowsAsync = async () => {
Promise.resolve().then(() => console.log("log in logsAsyncThrowsAsync"))
throw new Error("thrown in logsAsyncThrowsAsync")
}
const program = async () => {
try {
throwsSync()
} catch (e) {
// catches
console.log("catched1")
}
try {
throwsAsync()
} catch (e) {
// doesn't catch
console.log("catched2")
}
try {
logsSyncThrowsAsync()
} catch (e) {
// doesn't catch
console.log("catched3")
}
try {
logsAsyncThrowsAsync()
} catch (e) {
// doesn't catch
console.log("catched4")
}
try {
await throwsAsync()
} catch (e) {
// catches
console.log("catched5")
}
try {
await logsSyncThrowsAsync()
} catch (e) {
// catches
console.log("catched6")
}
}
program()