From e147b715613789d07a5cfe2f0458620dd53e3069 Mon Sep 17 00:00:00 2001 From: Suguru Motegi Date: Sun, 12 Nov 2017 16:11:25 -0800 Subject: [PATCH] fix(race): fix race not to resolve promise if task is empty --- lib/race.js | 3 +++ test/lib/test.race.js | 43 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/race.js b/lib/race.js index 06a809d7..46dd6e9e 100644 --- a/lib/race.js +++ b/lib/race.js @@ -7,6 +7,9 @@ class Race extends Parallel { constructor(collection) { super(collection); this._result = undefined; + if (this._rest === 0) { + this._rest = -1; + } } _callResolve(value) { diff --git a/test/lib/test.race.js b/test/lib/test.race.js index 21922f78..3e377bd4 100644 --- a/test/lib/test.race.js +++ b/test/lib/test.race.js @@ -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)); }); });