-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo12_async_promise_5.js
49 lines (44 loc) · 1.9 KB
/
Demo12_async_promise_5.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
/**
* * 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)
//? return or not return inside callback of then
const p_10 = new Promise((resolve, reject) => {
resolve(100)
})
p_10.then(data1 => {
console.log('P_10 is FIRST', data1)
return data1 + 100
})
.then(data2 => {
console.log('P_10 is SECOND', data2)
})
.then(data3 => {
console.log('P_10 is THIRD', data3) // data3 is undefine, because of inside function of data2 is not return value for data3
})
.catch(error => {
console.log(error)
})
.finally(() => {
console.log('DONE')
})