-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo12_async_promise_4.js
58 lines (50 loc) · 2.03 KB
/
Demo12_async_promise_4.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
/**
* * Resolve call-back hell problem
* * ES using PROMISE - Static methods of PROMISE
*/
'use strict'
console.clear()
//? Static methods
//! - Promise.resolve(value): return promise resolved (finish) with value
//! - Promise.reject(reason): return promise reject (error) with value of error
//! - Promise.all(iterable of multiple promises):
// + Input array of promises or non-promises objects (number, boolean, etc...)
// + Return new promise object.
// + Wait all of promises objects in array must be completed/resolved (finish) by order or promises in array
// + If we have one of promises to be rejected, this promise (rejected) will throw error, but others is continue
// + So, these promises are executed parallel
//! - Promise.race(iterable of multiple promises):
// + Input array of promises or non-promises objects (number, boolean, etc...)
// + Return new promise object.
// + Wait all of promises objects in array must be completed. Return of promise which is completed first will return the result (Resolved/Rejected)
//! - Promise.allSettled(iterable of multiple promises):
// + Input array of promises or non-promises objects (number, boolean, etc...)
// + Return new promise object.
// + Wait all of promises objects in array must be completed. Return array of result of all of promises
//! - Promise.any(iterable of multiple promises): experimental (beta)
//? Catch error and callback error of THEN
const resolvePromise = new Promise((resolve, reject) => {
resolve('Promise is resolved')
})
const rejectPromise = new Promise((resolve, reject) => {
reject('Promise is rejected')
})
rejectPromise
.then(() => {
throw new Error('Oh no no ERROR')
}, error => {
console.log('rejectPromise: ', error)
})
// resolvePromise
// .then(() => {
// throw new Error('Oh no no ERROR')
// }, error => {
// console.log('resolvePromise: ', error)
// })
resolvePromise
.then(() => {
throw new Error('Oh no no ERROR')
})
.catch(error => {
console.log('resolvePromise: ', error)
})