-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromiseMethod.js
More file actions
56 lines (46 loc) · 1.96 KB
/
PromiseMethod.js
File metadata and controls
56 lines (46 loc) · 1.96 KB
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
// Promise Methods
const p1 = new Promise(function (resolve, reject) {
// setTimeout(() => resolve('P1 Success'), 3000)
setTimeout(() => reject('P1 Failed'), 3000)
})
const p2 = new Promise(function (resolve, reject) {
// setTimeout(() => resolve('P2 Success'), 1000)
setTimeout(() => reject('P2 Failed'), 1000)
})
const p3 = new Promise(function (resolve, reject) {
// setTimeout(() => resolve('P3 Success'), 500)
setTimeout(() => reject('P3 Failed'), 500)
})
// Promise.all
// all the promise in the array have resolved. if any promise in the array reject, the returned promise
// immediately rejects with the reason of the first reject promise
// Promise.all([p1, p2, p3]).then(function (result) {
// console.log('result', result)
// }).catch(function (error) {
// console.log(error)
// // console.log(error.errors)
// })
// Promise.allSettled
// return a single promise that resolves when all the promise have settled, either fulfilled or rejected.
// the resulting promise resolves with an array of object each presenting the outcome of each promise.
// Promise.allSettled([p1, p2, p3]).then(function (result) {
// console.log(result)
// }).catch(function (error) {
// console.log(error)
// })
// Promise.race
// return a single promise that resolves or rejects as soon as one of the input promises resolves or rejects
// Promise.race([p1, p2, p3]).then(function (result) {
// console.log(result)
// }).catch(function (error) {
// console.log(error)
// })
// Promise.any
// return a single promise that resolves as soon as any one of the input promise resolve, if all the promise
// get rejects it return the rejections errors
Promise.any([p1, p2, p3]).then(function (result) {
console.log(result)
}).catch(function (error) {
console.log(error) // AggregateError: All promises were rejected
console.log(error.errors) // ['P1 Failed', 'P2 Failed', 'P3 Failed']
})