Skip to content

Commit 88ec35e

Browse files
committed
Added test for when and all
1 parent 8d6be4d commit 88ec35e

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

lib/promise.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@
100100
* @param {Array} promises a list of promises to wait for.
101101
* @return {AV.Promise} the new promise.
102102
*/
103-
when: function(promises, isAll) {
103+
when: function(promises) {
104104
// Allow passing in Promises as separate arguments instead of an Array.
105105
var objects;
106106
if (promises && AV._isNullOrUndefined(promises.length)) {
107107
objects = arguments;
108108
} else {
109109
objects = promises;
110110
}
111+
var isAll = _.last(arguments);
112+
isAll = AV._.isBoolean(isAll) ? isAll : false;
111113

112114
var total = objects.length;
113115
var hadError = false;
@@ -129,15 +131,17 @@
129131
var resolveOne = function(i) {
130132
total = total - 1;
131133
if(hadError && !promise._rejected && isAll) {
132-
return promise.reject(promise, errors[i]);
134+
promise.reject.call(promise, errors[i]);
135+
return;
133136
}
134137

135138
if (total === 0) {
136-
if (hadError && !isAll) {
137-
promise.reject(errors);
139+
if (hadError && !promise._rejected) {
140+
promise.reject.call(promise, errors);
138141
} else {
139142
if(isAll) {
140-
promise.resolve.call(promise, results);
143+
if(!promise._rejected)
144+
promise.resolve.call(promise, results);
141145
} else {
142146
promise.resolve.apply(promise, results);
143147
}
@@ -274,7 +278,7 @@
274278
resolve: function(result) {
275279
if (this._resolved || this._rejected) {
276280
throw "A promise was resolved even though it had already been " +
277-
(this._resolved ? "resolved" : "rejected") + ".";
281+
(this._resolved ? "resolved" : "rejected") + "." + result;
278282
}
279283
this._resolved = true;
280284
this._result = arguments;
@@ -294,16 +298,16 @@
294298
fn(function (value) {
295299
if (done) return;
296300
done = true;
297-
self.resolve(value);
301+
self.resolve.call(self, value);
298302
}, function (reason) {
299303
if (done) return;
300304
done = true;
301-
self.reject(reason);
305+
self.reject.call(self, reason);
302306
})
303307
} catch (ex) {
304308
if (done) return;
305309
done = true;
306-
self.reject(ex);
310+
self.reject.call(self, ex);
307311
}
308312
},
309313

test/promise.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ describe('promise', function() {
9999
});
100100
};
101101

102+
function timerPromisefyReject(delay) {
103+
return new AV.Promise(function (resolve, reject) {
104+
setTimeout(function () {
105+
reject(delay);
106+
}, delay);
107+
});
108+
};
109+
102110

103111
describe('AV.Promise.all and AV.Promise.when', function() {
104112
it('AV.Promise.all is resolved with array', function(done){
@@ -138,6 +146,48 @@ describe('promise', function() {
138146
done();
139147
});
140148
});
149+
150+
it('AV.Promise.when is rejected with errors', function(done){
151+
var startDate = Date.now();
152+
153+
AV.Promise.when(
154+
timerPromisefyReject(1),
155+
timerPromisefyReject(32),
156+
timerPromisefyReject(64),
157+
timerPromisefyReject(128)
158+
).catch(function (errors) {
159+
expect(errors.length).to.be(4);
160+
expect(errors[0]).to.be(1);
161+
expect(errors[1]).to.be(32);
162+
expect(errors[2]).to.be(64);
163+
expect(errors[3]).to.be(128);
164+
//should be 128 ms
165+
expect(Date.now() - startDate).to.be.within(125,140);
166+
167+
done();
168+
}).done(function(ret){
169+
throw ret;
170+
});
171+
});
172+
173+
it('AV.Promise.all is rejected with only one error', function(done){
174+
var startDate = Date.now();
175+
176+
AV.Promise.all([
177+
timerPromisefyReject(1),
178+
timerPromisefyReject(32),
179+
timerPromisefyReject(64),
180+
timerPromisefyReject(128)
181+
]).catch(function (error) {
182+
expect(error).to.be(1);
183+
//should be 1 ms
184+
expect(Date.now() - startDate).to.be.within(0,5);
185+
setTimeout(done, 500);
186+
}).done(function(ret){
187+
throw ret;
188+
});
189+
});
190+
141191
});
142192

143193
describe('AV.Promise.race', function(){
@@ -157,8 +207,8 @@ describe('promise', function() {
157207
});
158208
it('should run all promises.', function(done) {
159209
var results = [];
160-
function timerPromisefy2(delay) {
161-
return new AV.Promise(function (resolve) {
210+
var timerPromisefy2 = function(delay) {
211+
return new AV.Promise(function (resolve, reject) {
162212
setTimeout(function () {
163213
results.push(delay);
164214
resolve(delay);
@@ -176,6 +226,8 @@ describe('promise', function() {
176226
if (wasCalled) throw 'error';
177227
wasCalled = true;
178228
expect(value).to.be(1);
229+
}).catch(function(error) {
230+
throw error;
179231
});
180232
setTimeout(function() {
181233
expect(wasCalled).to.be(true);
@@ -187,5 +239,6 @@ describe('promise', function() {
187239
done();
188240
}, 500);
189241
});
242+
190243
});
191244
});

0 commit comments

Comments
 (0)