-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter14_promise_catch.js
44 lines (40 loc) · 1.42 KB
/
chapter14_promise_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
function getName() {
return new Promise((resolve, reject)=> {
// reject('an error is occurred');
// throw new Error('an error is occurred');
// throw new Exception('an error is occurred');
const obj = {};
obj.x.y.d;
})
}
getName()
.then(
name => {
console.log(name)
}
)
.catch(
err => {
// console.log(err) // 1. an error is occurred]
// console.log(err) // 2. [Error: an error is occurred]
// console.log(err) // 3. [ReferenceError: Exception is not defined]
console.log(err) // 4. [[TypeError: Cannot read property 'y' of undefined]]
}
);
/*
* Write a Promise
* Implement a function which returns a promise which
* resolve when the parameter passed in it true,
* and rejects when the parameter passed in is falls
*/
function getIdentityPromise(shouldResolve) {
// TODO: Implement me to return a resolved project when shouldResolve is true,
// and return a rejected promise when shouldResolve is false
return new Promise( (resolve, reject) => {
shouldResolve ? resolve(true) : reject(false);
})
}
getIdentityPromise(true)
.then(result => {
console.log(result);
})