Skip to content

Commit

Permalink
fix: empty input empty output
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Mar 11, 2020
1 parent 870329c commit 96755ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ language: node_js
node_js:
- '8'
- '10'
before_install:
- npm i npminstall -g
install:
- npm i npminstall && npminstall
- npminstall
script:
- npm run ci
after_script:
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ module.exports = function (funcs, n) {
if (funcs.some(func => func && typeof func.then === 'function')) n = funcs.length;

return new Promise(resolve => {
if (funcs.length === 0) return resolve([]);

const results = [];
let index = 0;
let finish = 0;

for (let i = 0; i < n; i++) {
run();
}

function run() {
const i = index++;
if (i >= funcs.length) return;
let ins = funcs[i];
if (!ins) return;
if (!ins) {
results[i] = undefined;
if (++finish >= funcs.length) return resolve(results);
return;
}

if (typeof ins === 'function') ins = ins();
if (ins && typeof ins.next === 'function' && typeof ins.throw === 'function') ins = require('co')(ins);
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ describe('co-gather', () => {
// notice: promise will start at once
around(use, 300);
});

it('should work with empty array', async () => {
const res = await gather([]);
assert.deepEqual(res, []);
});

it('should work with undefined item', async () => {
const res = await gather([ undefined, afun(3, null, 200) ]);
assert.deepEqual(res, [ undefined, { value: 3, isError: false } ]);
});
});

function around(a, b) {
Expand Down

0 comments on commit 96755ff

Please sign in to comment.