Skip to content

Commit

Permalink
fix(race): fix race not to resolve promise if task is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru03 committed Nov 13, 2017
1 parent 8abf1e6 commit e147b71
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
3 changes: 3 additions & 0 deletions lib/race.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Race extends Parallel {
constructor(collection) {
super(collection);
this._result = undefined;
if (this._rest === 0) {
this._rest = -1;
}
}

_callResolve(value) {
Expand Down
43 changes: 35 additions & 8 deletions test/lib/test.race.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,49 @@ parallel('race', () => {
});
});

it('should return undefined if tasks is an empty array', () => {
it('should be pending if tasks is an empty array', () => {

return Aigle.race([])
.then(res => assert.strictEqual(res, undefined));
let called = false;
Aigle.race([])
.then(() => called = true);
return Aigle.delay(DELAY)
.then(() => assert.strictEqual(called, false));
});

it('should return undefined if tasks is an empty object', () => {
it('should be pending if tasks is an empty object', () => {

return Aigle.race({})
.then(res => assert.strictEqual(res, undefined));
let called = false;
Aigle.race({})
.then(() => called = true);
return Aigle.delay(DELAY)
.then(() => assert.strictEqual(called, false));
});

it('should return undefined if tasks is empty', () => {

return Aigle.race()
.then(res => assert.strictEqual(res, undefined));
let called = false;
Aigle.race()
.then(() => called = true);
return Aigle.delay(DELAY)
.then(() => assert.strictEqual(called, false));
});

it('should return a resolved promise', () => {
return Aigle.race([
Aigle.race(),
Aigle.resolve(1),
2
])
.then(res => assert.strictEqual(res, 1));
});

it('should return a value', () => {
return Aigle.race([
Aigle.race(),
1,
Aigle.resolve(2)
])
.then(res => assert.strictEqual(res, 1));
});
});

Expand Down

0 comments on commit e147b71

Please # to comment.