Skip to content

Commit

Permalink
Fix: Properly cancel pEventIterator when resolutionEvents emits …
Browse files Browse the repository at this point in the history
…item not matching filter (#43)
  • Loading branch information
MoLow authored Jun 14, 2023
1 parent 487bc28 commit 93d16fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export function pEventIterator(emitter, event, options) {

// eslint-disable-next-line unicorn/no-array-callback-reference
if (options.filter && !options.filter(value)) {
cancel();
return;
}

Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,18 @@ test('resolve event resolves pending promises and finishes the iterator', async
await t.deepEqual(await iterator.next(), {done: true, value: undefined});
});

test('resolve event resolves pending promises and finishes the iterator - when filter is set', async t => {
const emitter = new EventEmitter();
const iterator = pEventIterator(emitter, '🦄', {resolutionEvents: ['end'], filter: Boolean});

(async () => {
await delay(200);
emitter.emit('end');
})();

await t.deepEqual(await iterator.next(), {done: true, value: undefined});
});

test('.multiple()', async t => {
const emitter = new EventEmitter();

Expand Down

0 comments on commit 93d16fb

Please sign in to comment.