-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.html
182 lines (162 loc) · 5.02 KB
/
promise.html
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
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
// { //promise 0
// function ajax() {
// var succ_f = null,
// fail_f = null,
// obj = {
// success: function (callback) {
// succ_f = callback;
// },
// fail: function (callback) {
// fail_f = callback;
// }
// };
// setTimeout(function () {
// var num = Math.random()
// if (num < .5) {
//
// succ_f && succ_f();
// }
// else {
//
// fail_f && fail_f();
// }
// }, 1000);
// return obj;
// }
// var aj = ajax();
// aj.success(function () {
// console.log('suc')
// });
// aj.fail(function () {
// console.log('fail')
// });
// }
// promise
function Promise(f) {
var that = this;
this.suc_callBacks = this.suc_callBacks || [];
this.fail_callBack = function () {
};
var suc = function (e) {
var fun = (that.suc_callBacks.length && that.suc_callBacks.shift() ) || function () {
},
returnVal = fun(e);
if (returnVal instanceof Promise) {
returnVal.suc_callBacks = that.suc_callBacks;
returnVal.fail_callBack = that.fail_callBack;
}
},
fail = function (e) {
that.fail_callBack(e);
};
this.then = function (f) {
that.suc_callBacks.push(f);
return that;
};
this.catch = function (f) {
that.fail_callBack = f;
};
setTimeout(function () {
f(suc, fail);
}, 0)
}
function test(resolve, reject) {
setTimeout(function () {
var num = Math.random();
if (num < .5) {
resolve('succ');
}
else {
reject('404');
}
}, 1000);
}
new Promise(test).then(function (re) {
console.log(re)
}).catch(function (e) {
console.log('siba:' + e)
});
Promise.all = function (arr) {
if (!arr instanceof Array) return;
var counter = 0,
total = arr.length,
i = 0,
result = [],
callback = function () {
},
failCb = function () {
};
for (; i < total; i++) {
arr[i].then(function (e) {
counter++;
result.push(e);
if (counter === total) {
callback(result);
}
}).catch(function (e) {
failCb(e);
})
}
return {
then: function (cb) {
callback = cb;
return this;
}, catch: function (cb) {
failCb = cb;
}
}
};
Promise.resolve = function (f) {
}
var p1 = new Promise(function (resolve, reject) {
setTimeout(resolve, 500, 'P1');
});
var p2 = new Promise(function (resolve, reject) {
setTimeout(reject, 600, 'P2');
});
// 同时执行p1和p2,并在它们都完成后执行then:
Promise.all([p1, p2]).then(function (results) {
console.log(results); // 获得一个Array: ['P1', 'P2']
}).catch(function () {
console.log('ff')
});
// // 0.5秒后返回input*input的计算结果:
// function multiply(input) {
// return new Promise(function (resolve, reject) {
// console.log('calculating ' + input + ' x ' + input + '...');
// setTimeout(resolve, 500, input * input);
// });
// }
//
// // 0.5秒后返回input+input的计算结果:
// function add(input) {
// return new Promise(function (resolve, reject) {
// console.log('calculating ' + input + ' + ' + input + '...');
// setTimeout(resolve, 500, input + input);
// });
// }
//
// new Promise(function (resolve, reject) {
// console.log('start new Promise...');
// resolve(123);
// })
// .then(multiply)
// .then(add)
// .then(multiply)
// .then(add)
// .then(function (result) {
// console.log('Got value: ' + result);
// }).catch(function (e) {
// console.error('err:' + e);
// })
</script>
</body>
</html>