promise

promise是一个类,高版本的浏览器已经实现了promise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//=> executor
//=> executor 包含两个参数 分别是resolve(解决) reject(拒绝)
//=> 代码一旦成功就不会走向失败

let fs = require('fs');
let p = new Promise(function(resolve, reject){ //=> 分别是两个函数
resolve('ok'); //=> 我们自己决定是成功还是失败
fs.readFile('test.txt', 'utf8', function(err, data){
if(err) reject(err);
resolve(data);
})
});

//=> p 代表promise实例
p.then(function(data){ //=> 成功的回调
console.log(data);
},function(err){ //=> 失败的回调
console.log(err)
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let p = new Promise((resolve, reject)=>{
setTimeout(()=>{
if(Math.random() > 0.5){
resolve('yes');
}else{
reject('no');
}
}, 1000)
});

p.then(function(data){
console.log(data);
},function(err){
console.log(err);
});

Promise 三个状态 pending fulfilled rejected

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
//=> promise 简单实现

function Promise(executor){
let self = this;
self.status = 'pending';
self.value = undefined;
self.reason = undefined;
self.onResolvedCallbacks = [];
self.onRejectedCallbacks = [];
function resolve(value){
if(self.status === 'pending'){
self.value = value;
self.status = 'resolved';
self.onResolvedCallbacks.forEach(item=>item(self.value));
}
}
function reject(reason){
if(self.status === 'pending'){
self.reason = reason;
self.status = 'rejected';
self.onRejectedCallbacks.forEach(item=>item(self.reason));
}
}
try{
executor(resolve, reject)
}catch(e){ //=> 有错误会走向失败
reject(e);
}
}
Promise.prototype.then = function(onFulfilled, onRejected){
let self = this;
if(self.status === 'resolved'){
onFulfilled(self.value);
}
if(self.status === 'rejected'){
onRejected(self.reason);
}
if(self.status === 'pending'){
self.onResolvedCallbacks.push(onFulfilled);
self.onRejectedCallbacks.push(onRejected);
}
}
module.exports = 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
//=> Promise 链式调用

function Promise(executor){
let self = this;
self.status = 'pending';
self.value = undefined;
self.reason = undefined;
self.onResolvedCallbacks = [];
self.onRejectedCallbacks = [];
function resolve(value){
if(self.status === 'pending'){
self.value = value;
self.status = 'resolved';
self.onResolvedCallbacks.forEach(item=>item(self.value));
}
}
function reject(reason){
if(self.status === 'pending'){
self.reason = reason;
self.status = 'rejected';
self.onRejectedCallbacks.forEach(item=>item(self.reason));
}
}
try{
executor(resolve, reject)
}catch(e){ //=> 有错误会走向失败
reject(e);
}
}
Promise.prototype.then = function(onFulfilled, onRejected){
let self = this;
let promise2;
if(self.status === 'resolved'){
return promise2 = new Promise(function(resolve, reject){
let x = onFulfilled(self.value);
if(x instanceof Promise){
x.then(resolve, reject)
}else{
resolve(x);
}
})
}
if(self.status === 'rejected'){
return promise2 = new Promise(function(resolve, reject){
let x = onRejected(self.reason);
if(x instanceof Promise){
x.then(resolve, reject)
}else{
reject(x);
}
})
}
if(self.status === 'pending'){
return promise2 = new Promise(function(resolve, reject){
self.onResolvedCallbacks.push(function(){
let x = onFulfilled(self.value);
if(x instanceof Promise){
x.then(resolve, reject)
}else{
resolve(x);
}
});
self.onRejectedCallbacks.push(function(){
let x = onRejected(self.reason);
if(x instanceof Promise){
x.then(resolve, reject)
}else{
reject(x);
}
});
})

}
}
module.exports = 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
//=> Promise 异常处理

function Promise(executor){
let self = this;
self.status = 'pending';
self.value = undefined;
self.reason = undefined;
self.onResolvedCallbacks = [];
self.onRejectedCallbacks = [];
function resolve(value){
if(self.status === 'pending'){
self.value = value;
self.status = 'resolved';
self.onResolvedCallbacks.forEach(item=>item(self.value));
}
}
function reject(reason){
if(self.status === 'pending'){
self.reason = reason;
self.status = 'rejected';
self.onRejectedCallbacks.forEach(item=>item(self.reason));
}
}
try{
executor(resolve, reject)
}catch(e){ //=> 有错误会走向失败
reject(e);
}
}
Promise.prototype.then = function(onFulfilled, onRejected){
onFulfilled = typeof oFulfilled === 'function'?onFulfilled:function(data){return data};
onRejected = typeof onRejected === 'function'?onRejected:function(data){return data};
let self = this;
let promise2;
if(self.status === 'resolved'){
return promise2 = new Promise(function(resolve, reject){
try{
let x = onFulfilled(self.value);
if(x instanceof Promise){
x.then(resolve, reject)
}else{
resolve(x);
}
}catch(e){
reject(e);
}

})
}
if(self.status === 'rejected'){
return promise2 = new Promise(function(resolve, reject){
try{
let x = onRejected(self.reason);
if(x instanceof Promise){
x.then(resolve, reject)
}else{
reject(x);
}
}catch(e){
reject(e);
}

})
}
if(self.status === 'pending'){
return promise2 = new Promise(function(resolve, reject){
self.onResolvedCallbacks.push(function(){
let x = onFulfilled(self.value);
if(x instanceof Promise){
x.then(resolve, reject)
}else{
resolve(x);
}
});
self.onRejectedCallbacks.push(function(){
let x = onRejected(self.reason);
if(x instanceof Promise){
x.then(resolve, reject)
}else{
reject(x);
}
});
})

}
}
Promise.prototype.catch = function(func){
return this.then(null, func);
}
module.exports = 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
//=> Promise与其他库结合使用
function Promise(executor){
let self = this;
self.status = 'pending';
self.value = undefined;
self.reason = undefined;
self.onResolvedCallbacks = [];
self.onRejectedCallbacks = [];
function resolve(value){
if(self.status === 'pending'){
self.value = value;
self.status = 'resolved';
self.onResolvedCallbacks.forEach(item=>item(self.value));
}
}
function reject(reason){
if(self.status === 'pending'){
self.reason = reason;
self.status = 'rejected';
self.onRejectedCallbacks.forEach(item=>item(self.reason));
}
}
try{
executor(resolve, reject)
}catch(e){ //=> 有错误会走向失败
reject(e);
}
}
Promise.prototype.then = function(onFulfilled, onRejected){
onFulfilled = typeof oFulfilled === 'function'?onFulfilled:function(data){return data};
onRejected = typeof onRejected === 'function'?onRejected:function(data){return data};
let self = this;
let promise2;
if(self.status === 'resolved'){
return promise2 = new Promise(function(resolve, reject){
try{
let x = onFulfilled(self.value);
resolvePromise(promise2, x, resolve, reject);
}catch(e){
reject(e);
}

})
}
if(self.status === 'rejected'){
return promise2 = new Promise(function(resolve, reject){
try{
let x = onRejected(self.reason);
resolvePromise(promise2, x, resolve, reject);
}catch(e){
reject(e);
}

})
}
if(self.status === 'pending'){
return promise2 = new Promise(function(resolve, reject){
self.onResolvedCallbacks.push(function(){
let x = onFulfilled(self.value);
resolvePromise(promise2, x, resolve, reject);
});
self.onRejectedCallbacks.push(function(){
let x = onRejected(self.reason);
resolvePromise(promise2, x, resolve, reject);
});
})

}
}
function resolvePromise(promise2, x, resolve, reject){
//=> 将返回的promise不停的执行,直到失败或者返回一个普通的数据类型
if(promise2 === x){
throw new TypeError('xxxx');
}
if((x !== null) && (typeof x === 'function') || (typeof x === 'object')){
//=> promise
let x = x.then;
if(typeof x === 'function'){
then.call(x, fuunction(value){
resolvePromise(promise2, value, resolve, reject);
},function(reason){
reject(reason);
})
}
}else{
resolve(x);
}
}
Promise.prototype.catch = function(func){
return this.then(null, func);
}
Promise.all = function(promises){
return new Promise(function(resolve, reject){
let result = [];
let resolved = function(index){
return function(data){
index++;
if(i === promises.length){
resolve(result);
}
}
}
for(let i = 0;i< promises.length; i+=1){
promises[i].then(resolved(i));
}
})
}
module.exports = Promise