-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
asyncIteratorToAsyncIterable
(#19)
* Add `asyncIteratorToAsyncIterable` * Typo
- Loading branch information
Showing
11 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/iter-fest/src/asyncIteratorToAsyncIterable.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { asyncIteratorToAsyncIterable } from './asyncIteratorToAsyncIterable'; | ||
|
||
describe('passing an async iterator-compatible generator', () => { | ||
let asyncIterable: AsyncIterableIterator<number>; | ||
|
||
beforeEach(() => { | ||
const iterate = (): AsyncIterator<number> => { | ||
let value = 0; | ||
|
||
return { | ||
next: (): Promise<IteratorResult<number>> => | ||
Promise.resolve(++value <= 3 ? { done: false, value } : { done: true, value: undefined }) | ||
}; | ||
}; | ||
|
||
asyncIterable = asyncIteratorToAsyncIterable(iterate()); | ||
}); | ||
|
||
test('should be iterable', async () => { | ||
const values: number[] = []; | ||
|
||
for await (const value of asyncIterable) { | ||
values.push(value); | ||
} | ||
|
||
expect(values).toEqual([1, 2, 3]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function asyncIteratorToAsyncIterable<T>(asyncIterator: AsyncIterator<T>): AsyncIterableIterator<T> { | ||
const asyncIterableIterator: AsyncIterableIterator<T> = { | ||
[Symbol.asyncIterator]: () => asyncIterableIterator, | ||
next: asyncIterator.next.bind(asyncIterator), | ||
...(asyncIterator.return ? { return: asyncIterator.return.bind(asyncIterator) } : {}), | ||
...(asyncIterator.throw ? { throw: asyncIterator.throw.bind(asyncIterator) } : {}) | ||
}; | ||
|
||
return asyncIterableIterator; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters