Skip to content

Commit

Permalink
Make .iterator() implement return() correctly (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng authored and sindresorhus committed Feb 11, 2019
1 parent db3203f commit 68d745a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ module.exports.iterator = (emitter, event, options) => {
},
return(value) {
cancel();
return {done, value};
return Promise.resolve({done, value});
}
};
};
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ test('event to AsyncIterator', async t => {
t.deepEqual(await iterator.next(), {done: false, value: 'Some third thing.'});
});

test('event to AsyncIterator implements return', async t => {
const emitter = new EventEmitter();
const iterator = pEvent.iterator(emitter, '🦄');

t.true(iterator.return('x') instanceof Promise);
t.deepEqual(await iterator.return('y'), {done: true, value: 'y'});
t.deepEqual(await iterator.next(), {done: true, value: undefined});
});

test('event to AsyncIterator with multiple event names', async t => {
const emitter = new EventEmitter();
const iterator = pEvent.iterator(emitter, ['🦄', '🌈']);
Expand Down

0 comments on commit 68d745a

Please sign in to comment.