Skip to content

Commit c88e754

Browse files
Shuwen QianDan Lasky
authored andcommitted
Modify requestqueue to use es6-promise
1 parent 943d0e0 commit c88e754

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

src/shared/js/requestqueue.html

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<link rel="import" href="zousan.html"/>
1+
<link rel="import" href="es6-promise.html"/>
2+
<!-- <link rel="import" href="zousan.html"/> -->
23
<script>
34
/**
45
* @license
@@ -17,7 +18,11 @@
1718
this.flight = [];
1819
this.concurrency = concurrency;
1920
this.processHook = hook;
20-
this.promise = new Zousan();
21+
this.defer = {};
22+
this.promise = new Promise(function(resolve, reject) {
23+
this.defer.resolve = resolve;
24+
this.defer.reject = reject;
25+
}.bind(this));
2126

2227
}
2328

@@ -34,14 +39,14 @@
3439
},this);
3540

3641
if (chunk && chunk.length > 0) {
37-
var all = Zousan.all(chunk);
42+
var all = Promise.all(chunk);
3843
all.then(this.next.bind(this), this.fail.bind(this));
3944
} else {
40-
this.promise.resolve(this.results);
45+
this.defer.resolve(this.results);
4146
}
4247
},
4348
fail: function(val) {
44-
this.promise.reject(val);
49+
this.defer.reject(val);
4550
this.failures.push(val);
4651
},
4752
exec: function() {
@@ -52,11 +57,11 @@
5257
this.flight.forEach(function(r) {
5358
r.abort();
5459
});
55-
this.promise.reject(new Error("Aborted"));
60+
this.defer.reject(new Error("Aborted"));
5661
}
5762
};
5863

5964
scope.RequestQueue = RequestQueue;
6065

61-
})(window.StrandLib = window.StrandLib || {});
62-
</script>
66+
})(window.StrandLib = window.StrandLib || {});
67+
</script>

test/lib_requestqueue.html

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,41 @@
1212
<body>
1313
<script>
1414
function Req() {
15-
this.promise = new Zousan();
15+
this.promise = new Promise(function(resolve) {
16+
resolve({response:"yes"});
17+
});
1618
}
1719
Req.prototype = {
1820
exec: function() {
19-
this.promise.resolve({response:"yes"});
2021
return this.promise;
2122
}
2223
};
2324

2425
function FReq() {
25-
this.promise = new Zousan();
26+
this.promise = new Promise(function(resolve, reject) {
27+
reject({response:"no"});
28+
});
2629
}
2730
FReq.prototype = {
2831
exec: function() {
2932
this.promise.then(function(){},function(){});
30-
this.promise.reject({response:"no"});
3133
return this.promise;
3234
}
3335
};
3436
function AReq() {
35-
this.promise = new Zousan();
37+
this.promise = new Promise(function(resolve) {
38+
resolve({response:"yes"});
39+
});
3640
}
3741
AReq.prototype = {
3842
exec: function() {
39-
this.promise.resolve({response:"yes"});
4043
return this.promise;
4144
},
4245
abort: sinon.spy()
4346
};
4447

4548
describe("RequestQueue", function() {
46-
49+
4750
it("should exist", function() {
4851
StrandLib.RequestQueue.should.be.a.function;
4952
});
@@ -137,4 +140,4 @@
137140
});
138141
</script>
139142
</body>
140-
</html>
143+
</html>

0 commit comments

Comments
 (0)