-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
64 lines (51 loc) · 1.33 KB
/
test.js
File metadata and controls
64 lines (51 loc) · 1.33 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
57
58
59
60
61
62
63
64
const Promise = require('./promise')
const myPromise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('success1!!!')
}, 2000)
})
const myPromise2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('success2!!!')
}, 3000)
})
Promise.all([myPromise1, myPromise2, 123]).then(res => {
console.log(res, 'all') // ['success1!!!', 'success2!!!']
}, err => {
console.log(err)
})
Promise.race([myPromise1, myPromise2]).then(res => {
console.log(res, 'race') // success1!!!
})
// catch test
const myPromise3 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('hahaha')
}, 1000)
})
myPromise3.catch(err => {
console.log(err) // error occurred
}).then(res => {
console.log(res, 456)
}, err => {
console.log(err, 123)
})
// then test
const myPromise4 = new Promise((resolve, reject) => {
resolve(myPromise3)
})
myPromise4.then(res => {
console.log(res) // 1
}).then().then().then(res => {
console.log(res, 'res') // 1
})
myPromise4.then(res => {
console.log(res) // 1
return new Promise(resolve => resolve(2))
}).then(res => {
console.log(res, 'res') // 2
})
myPromise4.then()
.finally(() => console.log('finally'))
.then(res => console.log(res)) // 1
Promise.resolve(myPromise4).then(res => console.log(res, 'resolve'))