promise-implement

手动实现了符合promiseA+规范的promise库

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function Promise(executor){
let self = this;
self.status = 'pending';
self.value = undefined;
self.onResolvedCallbacks = [];
self.onRejectedCallbacks = [];
function resolve(value){
setTimeout(() => {
if(self.status === 'pending'){
self.value = value;
self.status = 'resolved';
self.onResolvedCallbacks.forEach(fn=>fn(value));
}
}, 0);
}
function reject(value){
setTimeout(() => {
if(self.status === 'pending'){
self.value = value;
self.status = 'rejected';
self.onRejectedCallbacks.forEach(fn=>fn(value));
}
}, 0);
}

try {
executor(resolve,reject);
} catch (error) {
reject(error);
}
}

function resolvePromise(promise2,x,outerResolve,outerReject){
if(x=== promise2){
return outerReject(new TypeError('循环引用'));
}
let then,called;
if(x!=null && (typeof x === 'object' || typeof x === 'function')){
try {
then = x.then;
if(typeof then === 'function'){//如果是then里返了新promise,则用那个then的onfull,onreject来决定下一个
then.call(x,y=>{
if(called){
return;
}
called = true;
resolvePromise(promise2,y,outerResolve,outerReject);
},r=>{
if(called){
return;
}
called = true;
outerReject(r);
})
} else {
outerResolve(x);
}
} catch (e) {
if(called){
return;
}
called = true;
outerReject(e);
}
} else {
outerResolve(x);
}
}

Promise.prototype.catch = function (onRejected) {
return this.then(null, onRejected);
}
Promise.prototype.then = function(onFulfilled,onRejected){
let self = this;
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : (value)=>value;
onRejected = typeof onRejected === 'function' ? onRejected : (err)=>{throw err}
let promise2;
if(this.status === 'pending'){
promise2 = new Promise((resolve,reject)=>{
self.onResolvedCallbacks.push(function(value){
try {
let x = onFulfilled(value);
resolvePromise(promise2,x,resolve,reject);
} catch (e) {
reject(e);
}
});
self.onRejectedCallbacks.push(function(value){
try {
let x = onRejected(value);
resolvePromise(promise2,x,resolve,reject);
} catch (error) {
reject(error);
}
});
});
}
if(self.status === 'resolved'){
promise2 = new Promise((resolve,reject)=>{
setTimeout(() => {
try {
let x = onFulfilled(this.value);
resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
});
}
if(self.status === 'rejected'){
promise2 = new Promise((resolve,reject)=>{
setTimeout(() => {
try {
let x = onRejected(this.value);
resolvePromise(promise2,x,resolve,reject)
} catch (error) {
reject(error);
}
}, 0);
});
}
return promise2;

}
Promise.deferred = Promise.defer = function () {
var defer = {};
defer.promise = new Promise(function (resolve, reject) {
defer.resolve = resolve;
defer.reject = reject;
})
return defer;
}

Promise.race = function(arr){
let len = arr.length;
return new Promise((resolve,reject)=>{
for(let i=0;i<len;i++){
arr[i].then(res=>{
resolve(res);
},err=>{
reject(err);
})
}
});
}
Promise.resolve = function(value){
return new Promise((resolve,reject)=>{
resolve(value);
})
}
Promise.all = function(arr){
let len = arr.length;
let results = [];
let count=0;
return new Promise((resolve,reject)=>{
for(let i=0;i<len;i++){
arr[i].then(res=>{
results[i] = res;
count++;
if(results.length === count){
resolve(results);
}
},err=>{
reject(err);
})
}
});

}

module.exports = Promise;