-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest3.js
103 lines (58 loc) · 2.01 KB
/
test3.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// error handling with promises
let alert = require('alert');
// fetch('/article/promise-chaining/user.json')
// .then(response => response.json())
// .then(user => fetch(`https://api.github.com/users/${user.name}`))
// .then(response => response.json())
// .then(githubUser => new Promise((resolve, reject) => {
// let img = document.createElement('img');
// img.src = githubUser.avatar_url;
// img.className = "promise-avatar-example";
// document.body.append(img);
// setTimeout(() => {
// img.remove();
// resolve(githubUser);
// }, 3000);
// }))
// .catch(error => alert(error.message));
// fetch('https://no-such-server.blabla')
// .then(response => response.json())
// .catch(err => alert(err))
// fetch('/article/promise-chaining/user.json')
// .then(response => response.json())
// .then(user => fetch(`https://api.github.com/users/${user.name}`))
// .then(response => response.json())
// .then(githubUser => new Promise((resolve, reject) => {
// let img = document.createElement('img');
// img.src = githubUser.avatar_url;
// img.className = "promise-avatar-example";
// document.body.append(img);
// setTimeout(() => {
// img.remove();
// resolve(githubUser);
// }, 3000);
// }))
// try catch
// new Promise((resolve, reject) => {
// reject(new Error("Whoops!"));
// }).catch(alert);
// // or
// new Promise((resolve, reject) => {
// throw new Error("Whoops!");
// }).catch(alert);
// new Promise((resolve, reject) => {
// resolve("ok");
// }).then((result) => {
// throw new Error("Whoops!");
// }).catch(alert);
// new Promise((resolve, reject) => {
// resolve("ok");
// }).then((result) => {
// blabla();
// }).catch(alert);
// catch --then
// new Promise((resolve, reject) => {
// throw new Error("Whoops!");
// }).catch(function(error) {
// alert("The error is handled, continue normally");
// }).then(() => alert("Next successful handler runs"));