function queue list var defer Deferred resolve while list length defer

 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
function queue( list ) {
var defer = $['Deferred']()['resolve']();
while (list.length > 0) {
defer = defer.then(list.shift());
}
}
function sync1(){
console.log("step 1");
}
function async1(){
var defer = $['Deferred']();
setTimeout(function(){
console.log("step 2");
defer.resolve(3)
}, 2000);
return defer['promise']();
}
function async2( N ){
var defer = $['Deferred']();
setTimeout(function(){
console.log("step", N);
defer.resolve();
}, 2000);
return defer['promise']();
}
function sync2(){
console.log("END!1111");
}
queue([
sync1,
async1,
async2,
sync2
])