-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathch12async.js
More file actions
1434 lines (1345 loc) · 40.6 KB
/
ch12async.js
File metadata and controls
1434 lines (1345 loc) · 40.6 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function throwError() {
throw new Error('ERROR');
}
try {
setTimeout(throwError, 3000);
} catch (e) {
alert(e);//这里的异常无法捕获
}
function test(count, ms) {
var c = 1;
var time = [new Date() * 1];
var id = setTimeout(function() {
time.push(new Date() * 1);
c += 1;
if (c <= count) {
setTimeout(arguments.callee, ms);
} else {
clearTimeout(id);
var tl = time.length;
var av = 0;
for (var i = 1; i < tl; i++) {
var n = time[i] - time[i - 1]; //收集每次与上一次相差的时间数
av += n;
}
alert(av / count); // 求取平均值
}
}, ms);
}
winod.onload = function() {
var id = setTimeout(function() {
test(100, 1);
clearTimeout(id);
}, 3000);
}
var orig_setTimeout = window.setTimeout;
window.setTimeout = function(fun, wait) {
if (wait < 15) {
orig_setTimeout(fun, wait);
} else {
var img = new Image();
img.onload = img.onerror = function() {
fun();
};
img.src = "data:,foo";
}
};
window.onload = function() {
var a = new Date - 0;
setTimeout(function() {
alert(new Date - a);
});
var flag = 0;
var b = new Date,
text = ""
var id = setInterval(function() {
flag++;
if (flag > 4) {
clearInterval(id)
console.log(text)
}
text += (new Date - b + " ");
b = new Date
})
}
setTimeout(function() {
alert([].slice.call(arguments));
}, 10, 1, 2, 4);
if (window.VBArray && !(document.documentMode > 9)) {
(function(overrideFun) {
window.setTimeout = overrideFun(window.setTimeout);
window.setInterval = overrideFun(window.setInterval);
})(function(originalFun) {
return function(code, delay) {
var args = [].slice.call(arguments, 2);
return originalFun(function() {
if (typeof code == 'string') {
eval(code);
} else {
code.apply(this, args);
}
}, delay);
}
}
);
}
Deferred = function(/* optional */ canceller) {
this.chain = [];
this.id = setTimeout("1")
this.fired = -1;
this.paused = 0;
this.results = [null, null];
this.canceller = canceller;
this.silentlyCancelled = false;
this.chained = false;
};
function curry(fn, scope, args) {
return function() {
var argv = [].concat.apply(args, arguments)
return fn.apply(scope, argv);
};
}
Deferred.prototype = {
//三种状态,未触发,触发成功,触发失败
state: function() {
if (this.fired == -1) {
return 'unfired';
} else if (this.fired === 0) {
return 'success';
} else {
return 'error';
}
},
//取消触发,类似于ajax的abort
cancel: function(e) {
if (this.fired == -1) { //只有未触发时才能cancel掉
if (this.canceller) {
this.canceller(this);
} else {
this.silentlyCancelled = true;
}
if (this.fired == -1) {
if (!(e instanceof Error)) {
e = new Error(e + "");
}
this.errback(e);
}
} else if ((this.fired === 0) && (this.results[0] instanceof Deferred)) {
this.results[0].cancel(e);
}
},
//这里决定是用哪个列队
_resback: function(res) {
this.fired = ((res instanceof Error) ? 1 : 0);
this.results[this.fired] = res;
if (this.paused === 0) {
this._fire();
}
},
//判定是否触发过
_check: function() {
if (this.fired != -1) {
if (!this.silentlyCancelled) {
throw new "此方法已经被调用过";
}
this.silentlyCancelled = false;
return;
}
},
//触发成功列队
callback: function(res) {
this._check();
if (res instanceof Deferred) {
throw new Error("Deferred instances can only be chained if they are the result of a callback");
}
this._resback(res);
},
//触发错误列队
errback: function(res) {
this._check();
if (res instanceof Deferred) {
throw new Error("Deferred instances can only be chained if they are the result of a callback");
}
if (!(res instanceof Error)) {
res = new Error(res + "");
}
this._resback(res);
},
//同时添加成功与错误回调
addBoth: function(a, b) {
b = b || a
return this.addCallbacks(a, b);
},
//添加成功回调
addCallback: function(fn) {
if (arguments.length > 1) {
var args = [].slice.call(arguments, 1);
fn = curry(fn, window, args);
}
return this.addCallbacks(fn, null);
},
//添加错误回调
addErrback: function(fn) {
if (arguments.length > 1) {
var args = [].slice.call(arguments, 1);
fn = curry(fn, window, args);
}
return this.addCallbacks(null, fn);
},
//同时添加成功回调与错误回调,后来Promise的then方法就是参考它设计
addCallbacks: function(cb, eb) {
if (this.chained) {
throw new Error("Chained Deferreds can not be re-used");
}
if (this.finalized) {
throw new Error("Finalized Deferreds can not be re-used");
}
this.chain.push([cb, eb]);
if (this.fired >= 0) {
this._fire();
}
return this;
},
//将列队的回调依次触发
_fire: function() {
var chain = this.chain;
var fired = this.fired;
var res = this.results[fired];
var self = this;
var cb = null;
while (chain.length > 0 && this.paused === 0) {
var pair = chain.shift();
var f = pair[fired];
if (f === null) {
continue;
}
try {
res = f(res);
fired = ((res instanceof Error) ? 1 : 0);
if (res instanceof Deferred) {
cb = function(res) {
self.paused--;
self._resback(res);
};
this.paused++;
}
} catch (err) {
fired = 1;
if (!(err instanceof Error)) {
try {
err = new Error(err + "");
} catch (e) {
alert(e)
}
}
res = err;
}
}
this.fired = fired;
this.results[fired] = res;
if (cb && this.paused) {
res.addBoth(cb);
res.chained = true;
}
}
};
deferred.chain = [[fn1, fn2], [fn3, fn4], [fn5, fn6]]
var d = new Deferred();
d.addCallback(myCallback);
d.addErrback(myErrback);
d.addBoth(myBoth);
d.addCallbacks(myCallback, myErrback);
function increment(value) {
console.log(value);
return value + 1;
}
var d = new Deferred();
d.addCallback(increment);
d.addCallback(increment);
d.addCallback(increment);
d.callback(1);
var d = new Deferred();
d.addCallback(function(a) {
console.log(a)
return 4
}).addBoth(function(a) {
console.log(a);
throw "抛错"
}, function(b) {
console.log(b)
return "xxx"
}).addBoth(function(a) {
console.log(a);
return "正常"
}, function(b) {
console.log(b + "!")
return "出错"
}).addBoth(function(a) {
console.log(a + " 回复正常");
return "正常2"
}, function(b) {
console.log(b + " 继续出错")
return "出错2"
})
d.callback(3)
var elapsed = (function() {
var start = null;
return function() {
if (!start)
start = Date.now();
return ((Date.now() - start) / 1000)
}
})();
console.log(elapsed(), "start");
var dl1, dl2;
dl1 = new DeferredList([
doXHR('/sleep.php?n=3').addCallback(function(res) {
console.log(elapsed(), "n=3", res, res.responseText);
return res.responseText;
}),
doXHR('/sleep.php?n=4').addCallback(function(res) {
console.log(elapsed(), "n=4", res, res.responseText);
return res.responseText;
}),
doXHR('/sleep.php?n=5').addCallback(function(res) {
console.log(elapsed(), "n=5", res, res.responseText);
return res.responseText;
}),
doXHR('/sleep.php?n=6').addCallback(function(res) {
console.log(elapsed(), "n=6", res, res.responseText);
return res.responseText;
})]).addCallback(function(res) {
console.log(elapsed(), "first DeferredList complete.", res);
return dl2 = new DeferredList([
doXHR('/sleep.php?n=1').addCallback(function(res) {
console.log(elapsed(), "n=1", res, res.responseText);
return res.responseText;
}),
doXHR('/sleep.php?n=2').addCallback(function(res) {
console.log(elapsed(), "n=2", res, res.responseText);
return res.responseText;
}), ]).addCallback(function(res) {
console.log(elapsed(), "second DeferredList complete.", res);
console.log(elapsed(), "last", res);
return res;
})
}).addCallback(function(res) {
console.log(elapsed(), "end", res, dl1, dl2);
});
Deferred.define();
next(function() {
/* 处理 */
})
var o = {}; //定义一个对象
Deferred.define(o);//把Deferred的方法加持到它上面,让o成为一个Deferred子类
for (var x in o)
alert(x);
// parallel
// wait
// next
// call
// loop
o.next(function() {
/* 处理 */
})
Deferred.next(function() {//在暗地里创建一个Deferred实例,然后我们直接"链"下去就行了
/* 处理 */
})
Deferred.next_default = function(fun) {
var d = new Deferred();
var id = setTimeout(function() {
clearTimeout(id);
d.call()
}, 0);
d.canceller = function() {
try {
clearTimeout(id)
} catch (e) {
}
};
if (fun)
d.callback.ok = fun;
return d;
};
Deferred.define();
next(function func1() {
alert(1)
})
.next(function func2() {
alert(2)
});
alert(3);
function Deferred() {
return (this instanceof Deferred) ? this.init() : new Deferred()
}
Deferred.ok = function(x) {
return x
};
Deferred.ng = function(x) {
throw x
};
Deferred.prototype = {
init: function() {
this._next = null;
this.callback = {
ok: Deferred.ok,
ng: Deferred.ng
};
return this;
},
next: function(fun) {
return this._post("ok", fun)
},
error: function(fun) {
return this._post("ng", fun)
},
call: function(val) {
return this._fire("ok", val)
},
fail: function(err) {
return this._fire("ng", err)
},
cancel: function() {
(this.canceller || function() {
})();
return this.init();
},
_post: function(okng, fun) {
this._next = new Deferred();
this._next.callback[okng] = fun;
return this._next;
},
_fire: function(okng, value) {
var next = "ok";
try {
value = this.callback[okng].call(this, value);
} catch (e) {
next = "ng";
value = e;
}
if (value instanceof Deferred) {
value._next = this._next;
} else {
if (this._next)
this._next._fire(next, value);
}
return this;
}
};
value = this.callback[okng].call(this, value);
//相当于d1.callback.ok.call(d1,null)
//相当于func1.call(d1,null)
//相当于d1.func1();
if (this._next)
this._next._fire(next, value);
//相当于if(d2)d2.fire(next,value)
//相当于if(d2)d2.fire("ok",null)
//相当于d2.call()
window.onload = function() {
//最好把Deferred放到window.onload中,因为当它的next用
//image.onload实现时会用document.body.appendChild(image)
var a, b, c;
Deferred.
next(function() {
console.log(this);
a = this;
console.log("a1")
})
.next(function() {
console.log(this);
b = this;
console.log(a !== b)
console.log("a2");
})
.next(function() {
console.log(this);
c = this;
console.log(b !== c)
console.log("a3");
console.log("===========");
console.log(a._next === b);
console.log(b._next === c);
})
}
Deferred.next(function() {
alert(1)
})
.wait(0)
.next(function() {
alert(2)
})
Deferred.wait = function(n) {
var d = new Deferred(), t = new Date();
var id = setTimeout(function() {
d.call((new Date()).getTime() - t.getTime());
}, n * 1000);
d.canceller = function() {
clearTimeout(id)
};
return d;
};
Deferred.register = function(name, fun) {
this.prototype[name] = function() {
var a = arguments;
return this.next(function() {
return fun.apply(this, a);
});
};
};
Deferred.register("wait", Deferred.wait);
Deferred.next(function fun1() { //产生Deferred对象d1,
alert(1)
})
.wait(0) // 这里相当于.next(function curry(){ return Deferred.wait(0) })
// 产生Deferred对象 d_hide与d_wait
.next(function fun2() { //产生Deferred对象d2
alert(2)
})
_fire = function(okng, value) {
var next = "ok"; //每次都尝试从成功列队执行
try { //决定是执行成功回调还是错误回调
value = this.callback[okng].call(this, value);
} catch (e) {
next = "ng";
value = e; //反正返回值
if (Deferred.onerror)
Deferred.onerror(e);
}
if (value instanceof Deferred) {
value._next = this._next;
} else { //执行链表中的下一个Deferred的_fire
if (this._next)
this._next._fire(next, value);
}
return this;
}
value = this.callback[okng].call(this, value);
就相当于
// value = d_hide.callback.ok.call(d_hide,value)
// value = curry.call(d_hide,value)
// value = (function curry(){ return Deferred.wait(0) }).call(d_hide,value)
// value = d_wait
if (value instanceof Deferred) {
value._next = this._next;
//相当于 d_wait._next = d_hide._next
//相当于 d_wait._next = d2
} else {
if (this._next)
this._next._fire(next, value);
}
var d = Deferred.next(function() {
console.log("0")
});
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach(function(index) {
d = d.wait(1).next(function() {
console.log(index + "!!")
})
});
Deferred.parallel(function() {
return 1
}, function() {
return 2
}, function() {
return 3
}).next(function(a) {
console.log(a)//[1,2,3]
})
window.onload = function() {
function ajax(url) {
var d = Deferred();
var delay = url.match(/time=(\d+)/)[1];
// 真正的实现应该如下:
// var xhr = new (self.XMLHttpRequest || ActiveXObject)("Microsoft.XMLHTTP");
// xhr.onreadystatechange = function() {
// if (ajax.readyState == 4 && ajax.status == 200) {
// d.call(ajax.responseText)
// }
// }
// xhr.onload = function() {
// d.call(ajax.responseText)
// }
// xhr.onerror = function() {
// d.fail(ajax.responseText)
// }
//xhr.open(。。。。。)
setTimeout(function() {
d.call(delay)
}, +delay);
return d
}
Deferred.parallel(
ajax("rubylovre?time=2000"),
ajax("rubylovre?time=3000"),
ajax("rubylovre?time=4000")).next(function(a) {
//大概等4秒
console.log(a) //[2000,3000,4000]
})
}
Deferred.parallel = function(dl) {
var isArray = false; //它可以放一个数组或对象,或N个函数或Deferred对象做参数
if (arguments.length > 1) {
dl = Array.prototype.slice.call(arguments);
isArray = true;
} else if (Array.isArray && Array.isArray(dl) || typeof dl.length == "number") {
isArray = true;
}
//并归用的Deferred
var ret = new Deferred(),
//收集结果
values = {},
//计数器
num = 0;
for (var i in dl) {
if (dl.hasOwnProperty(i)) {
(function(d, i) {
//统统转成Deferred对象
if (typeof d == "function")
dl[i] = d = Deferred.next(d);//转换为Deferred对象
d.next(function(v) {//然后让它们添加两个回调:next与error
values[i] = v;
if (--num <= 0) {
//到这一步,说明用户最初传入的函数都成功返回数据了,组成values
if (isArray) {
values.length = dl.length;
values = Array.prototype.slice.call(values, 0);
}
ret.call(values);
}
}).error(function(e) {
ret.fail(e);
});
num++;
})(dl[i], i);
}
}
//如果里面没有内容立即执行
if (!num)
Deferred.next(function() {
ret.call()
});
ret.canceller = function() {
for (var i in dl)
if (dl.hasOwnProperty(i)) {
dl[i].cancel();
}
};
return ret;
};
Deferred.loop(10, function(n) {
console.log(n);
return Deferred.wait(1);
});
Deferred.next_faster_way_readystatechange = (typeof window === 'object')
&& (location.protocol == "http:")
&& window.VBArray && function(fun) {// MSIE
var d = new Deferred();
var t = new Date().getTime();
//浏览器的并发请求数是有限的,在IE6、IE7中为2~4,IE8、IE9为6
//如果超出这数目,可能造成堵塞,必须待这几个处理完才继续处理
//因此这里添加一个阈值,如果上次与这次相隔超过150还没有处理完
//那么就退化到原始的setTimeout方法
if (t - arguments.callee._prev_timeout_called < 150) {
var cancel = false;
//创建一个script节点,加载一个不存在的资源来引发onerror
//由于旧版本IE不区分onerror与onload,都只会触发onreadystatechange
//那么就用onreadystatechange
//只要造成异步效果,让用户有足够时间绑定回调就行了
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "data:text/javascript,";
script.onreadystatechange = function() {
if (!cancel) {
d.canceller();
d.call();
}
};
//清掉事件与移出DOM树
d.canceller = function() {
if (!cancel) {
cancel = true;
script.onreadystatechange = null;
document.body.removeChild(script);
}
};
//由于这里的逻辑,我们的Deferred最好延迟到domReady或onload后执行
document.body.appendChild(script);
} else {
arguments.callee._prev_timeout_called = t;
var id = setTimeout(function() {
d.call()
}, 0);
d.canceller = function() {
clearTimeout(id)
};
}
if (fun)
d.callback.ok = fun;
return d;
};
Deferred.next_faster_way_Image = (typeof window === 'object')
&& (typeof(Image) != "undefined")
&& !window.opera && document.addEventListener && function(fun) {
// 用于Opera外的标准浏览器
var d = new Deferred();
var img = new Image();
//创建一个image加载一个不存在的图片(为了防止万分之一的图片存在的情况,onload也绑上了)
var handler = function() {
d.canceller();
d.call();
};
img.addEventListener("load", handler, false);
img.addEventListener("error", handler, false);
d.canceller = function() {
img.removeEventListener("load", handler, false);
img.removeEventListener("error", handler, false);
};
img.src = "data:image/png," + Math.random();
if (fun)
d.callback.ok = fun;
return d;
};
Deferred.next_tick = (typeof process === 'object')
&& (typeof process.nextTick === 'function') && function(fun) {
var d = new Deferred();
process.nextTick(function() {
d.call()
});
if (fun)
d.callback.ok = fun;
return d;
};
(function(jQuery) {
var
promiseMethods = "then done fail isResolved isRejected promise".split(" "),
//用于转换arguments对象为真正的数组
sliceDeferred = [].slice;
jQuery.extend({
//这是最早期的实现,Deferred还随大众那样是一个双链结构,只不过它的每一条链都由_Deferred实现
//_Deferred方法会返回一个_Deferred对象,它可以简单地理解为一个操作函数数组的特殊对象
_Deferred: function() {
var //函数列表
callbacks = [],
//这一个数组,用于保存传入的上下文对象与参数 [ context , args ]
fired,
// 判定是否在触发过程中
firing,
// 判定是否被消
cancelled,
//这是要返回的对象
deferred = {
done: function() {
if (!cancelled) { //cancel就不能再绑定了
//与jQuery其他API一样,允许太多种传参形式,因此这里需要各种处理
var args = arguments,
i,
length,
elem,
type,
_fired;
if (fired) {
_fired = fired;
fired = 0;
}
for (i = 0, length = args.length; i < length; i++) {
elem = args[i];
type = jQuery.type(elem);
if (type === "array") { //递归调用自身
deferred.done.apply(deferred, elem);
} else if (type === "function") {
callbacks.push(elem);
}
}
if (_fired) { //如果已经触发了,那么就立即执行它们
deferred.resolveWith(_fired[0], _fired[1]);
}
}
return this;
},
//触发时允许指定上下文与传参
resolveWith: function(context, args) {
if (!cancelled && !fired && !firing) {
args = args || [];
firing = 1;
try {
while (callbacks[0]) {
//注意这里,每次都是弹出一个回调,因此此对象不能反复利用
callbacks.shift().apply(context, args);
}
} finally {
fired = [context, args];
firing = 0;
}
}
return this;
},
//触发时允许指定传参
resolve: function() {
deferred.resolveWith(this, arguments);
return this;
},
// 判定是否调用了resolve或resolveWith
isResolved: function() {
return !!(firing || fired);
},
// 取消
cancel: function() {
cancelled = 1;
callbacks = [];
return this;
}
};
return deferred;
},
// 真正给人用的Deferred,一个双链对象,由两个_Deferred对象合体而成
Deferred: function(func) {
var deferred = jQuery._Deferred(),
failDeferred = jQuery._Deferred(),
promise;
// Add errorDeferred methods, then and promise
jQuery.extend(deferred, {
then: function(doneCallbacks, failCallbacks) {
deferred.done(doneCallbacks).fail(failCallbacks);
return this;
},
fail: failDeferred.done,
rejectWith: failDeferred.resolveWith,
reject: failDeferred.resolve,
isRejected: failDeferred.isResolved,
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
promise: function(obj) {
//这是一个单例方法,无论调用多少次,只会返回同一个对象
//它与promiseMethods有着相同的方法
if (obj == null) {
if (promise) {
return promise;
}
promise = obj = {};
}
var i = promiseMethods.length;
while (i--) {
obj[promiseMethods[i]] = deferred[promiseMethods[i]];
}
return obj;
}
});
//将这两个cancel放进列队目的是,一旦执行fail与done就不能用了
deferred.done(failDeferred.cancel).fail(deferred.cancel);
// 移除 cancel方法
delete deferred.cancel;
if (func) {
func.call(deferred, deferred);
}
return deferred;
},
when: function(firstParam) {
var args = arguments,
i = 0,
length = args.length,
count = length, //计数器
//如果只是传入一个Deferred/Promise对象,那么就直接利用此对象,否则生成一个新的Deferred
//此Promise对象(下称“并归Promise”)用于绑定下一个阶段的回调
deferred = length <= 1 && firstParam && jQuery.isFunction(firstParam.promise)
? firstParam : jQuery.Deferred();
function resolveFunc(i) {
return function(value) {
args[i] = arguments.length > 1 ? sliceDeferred.call(arguments, 0) : value;
if (!(--count)) {
//如果count减为零,那么就将这些结果放到下一个阶段绑定的回调执行
deferred.resolveWith(deferred, sliceDeferred.call(args, 0));
}
};
}
if (length > 1) {
for (; i < length; i++) {
//只对Deferred/Promise对象进行操作,因为只有它们有promise方法
if (args[i] && jQuery.isFunction(args[i].promise)) {
//绑定过渡用的resolveFunc方法,让它们决定“并归Promise”的触发
args[i].promise().then(resolveFunc(i), deferred.reject);
} else {
--count;
}
}
if (!count) { //如果为零立即执行
deferred.resolveWith(deferred, args);
}
} else if (deferred !== firstParam) {
//如果什么也不传,也立即执行
deferred.resolveWith(deferred, length ? [firstParam] : []);
} //换言之,只传一个Deferred/Promise对象是不会执行的,只会转成Promise对象
return deferred.promise();
}
});
})(jQuery);
var deferred = $.Deferred()
//注意,这里不能链起来
//因为Deferred()返回的是Deferred对象
//pipe()返回的是Promise对象
var promise = deferred.pipe(function(a) {
console.log(a); //5
return a * 2;
}).pipe(function(a) {
console.log(a); //10
return a * 4;
}).pipe(function(a) {
console.log(a); //40
})
console.log(deferred === promise); //false
deferred.resolve(5);
//这里则可以链起来,始终都是那个Deferred对象
$.Deferred()
.then(function(a) {
console.log(a); //10
return a * 2;
}).then(function(a) {
console.log(a); //10
return a * 4;
}).then(function(a) {
console.log(a); //10
}).resolve(10);
var stuff1 = function(deferred) {
setTimeout(function() {
console.log("Stuff #1 is done!");
deferred.resolve();
}, 1000);
};
var stuff2 = function(deferred) {
setTimeout(function() {
console.log("Stuff #2 is done!");
deferred.resolve();
}, 500);
};
var stuff3 = function(deferred) {
setTimeout(function() {
console.log("Stuff #3 is done!");
deferred.resolve();
}, 500);
};
$.when(
$.Deferred(stuff1),
$.Deferred(stuff2),
$.Deferred(stuff3)
).then(function() {
console.log("done!");
});
var a = $.Deferred();
a.done(function(x) {
console.log(x);
}).done(function(x) {
console.log(x);
});
a.resolve(1);
a.resolve(2);
console.log("============")
var b = $.Deferred();
b.progress(function(y) {
console.log(y);
}).progress(function(y) {
console.log(y);
});
b.notify(3);
b.notify(4);
/*
try{