1
+ function _Promise ( fn ) {
2
+ const self = this ;
3
+ self . status = 'pending' ; // 状态
4
+ self . data = undefined ; // 结果
5
+ self . onFulfilledCallback = [ ] ;
6
+ self . onRejectedCallback = [ ] ;
7
+
8
+ function resolve ( value ) {
9
+ self . status = 'fulfilled' ;
10
+ self . data = value ;
11
+ setTimeout ( ( ) => {
12
+ for ( const callback of self . onFulfilledCallback ) {
13
+ callback ( value )
14
+ }
15
+ } )
16
+ }
17
+
18
+ function reject ( reason ) {
19
+ self . status = 'rejected' ;
20
+ self . data = reason ;
21
+ setTimeout ( ( ) => {
22
+ for ( const callback of self . onRejectedCallback ) {
23
+ callback ( reason )
24
+ }
25
+ } )
26
+ }
27
+
28
+ try {
29
+ fn ( resolve , reject ) ;
30
+ } catch ( error ) {
31
+ reject ( error ) ;
32
+ }
33
+
34
+ }
35
+
36
+ _Promise . prototype . then = function ( onFulfilled , onRejected ) {
37
+ const self = this ;
38
+
39
+ onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : ( ) => { } ;
40
+ onRejected = typeof onRejected === 'function' ? onRejected : ( ) => { } ;
41
+
42
+ if ( self . status === 'fulfilled' ) {
43
+ return new _Promise ( ( resolve , reject ) => {
44
+ try {
45
+ const res = onFulfilled ( self . data ) ;
46
+ if ( res instanceof _Promise ) {
47
+ res . then ( resolve , reject ) ;
48
+ } else {
49
+ resolve ( res ) ;
50
+ }
51
+ } catch ( error ) {
52
+ reject ( error )
53
+ }
54
+ } ) ;
55
+ } else if ( self . status === 'rejected' ) {
56
+ return new _Promise ( ( resolve , reject ) => {
57
+ try {
58
+ const res = onRejected ( self . data ) ;
59
+ if ( res instanceof _Promise ) {
60
+ res . then ( resolve , reject ) ;
61
+ } else {
62
+ reject ( res ) ;
63
+ }
64
+ } catch ( error ) {
65
+ reject ( error )
66
+ }
67
+ } ) ;
68
+ } else if ( self . status === 'pending' ) {
69
+ return new _Promise ( ( resolve , reject ) => {
70
+ self . onFulfilledCallback . push (
71
+ function ( ) {
72
+ try {
73
+ const res = onFulfilled ( self . data ) ;
74
+ if ( res instanceof _Promise ) {
75
+ res . then ( resolve , reject ) ;
76
+ } else {
77
+ resolve ( res ) ;
78
+ }
79
+ } catch ( error ) {
80
+ reject ( error )
81
+ }
82
+ }
83
+ )
84
+ self . onRejectedCallback . push (
85
+ function ( ) {
86
+ try {
87
+ const res = onRejected ( self . data ) ;
88
+ if ( res instanceof _Promise ) {
89
+ res . then ( resolve , reject ) ;
90
+ } else {
91
+ reject ( res ) ;
92
+ }
93
+ } catch ( error ) {
94
+ reject ( error )
95
+ }
96
+ }
97
+ )
98
+ } ) ;
99
+ }
100
+ }
101
+
102
+ // 顺便实现一下catch方法
103
+ _Promise . prototype . catch = function ( onRejected ) {
104
+ return this . then ( null , onRejected ) ;
105
+ }
106
+
107
+
108
+ const p = new _Promise ( function ( resolve , reject ) {
109
+ setTimeout ( function ( ) {
110
+ resolve ( 1 ) ;
111
+ } , 2000 ) ;
112
+ } ) ;
113
+
114
+ p . then ( function ( v ) {
115
+ console . log ( v ) ;
116
+ return 2 ;
117
+ } ) . then ( function ( v ) {
118
+ console . log ( v ) ;
119
+ return new _Promise ( function ( resolve , reject ) {
120
+ setTimeout ( function ( ) {
121
+ resolve ( 3 ) ;
122
+ } , 3000 ) ;
123
+ } ) ;
124
+ } ) . then ( function ( v ) {
125
+ console . log ( v ) ;
126
+ } ) ;
0 commit comments