Created
May 30, 2017 20:07
-
-
Save mattpodwysocki/186cf6f34cfc65227dab1dc414a98e67 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
class MapAsyncIterable<T, R> implements AsyncIterable<R> { | |
private _source: Iterable<T | PromiseLike<T>> | AsyncIterable<T>; | |
private _selector: (value: T) => Promise<R> | R; | |
constructor( | |
source: Iterable<T | PromiseLike<T>> | AsyncIterable<T>, | |
selector: (value: T) => Promise<R> | R) { | |
this._source = source; | |
this._selector = selector; | |
} | |
async *[Symbol.asyncIterator]() { | |
for await (let item of <AsyncIterable<T>>(this._source)) { | |
let result = await this._selector(item); | |
yield result; | |
} | |
} | |
} | |
export function mapAsync<T, R>( | |
source: Iterable<T | PromiseLike<T>> | AsyncIterable<T>, | |
selector: (value: T) => Promise<R>, | |
thisArg?: any): AsyncIterable<R> { | |
const fn = selector.bind(thisArg); | |
return new MapAsyncIterable<T, R>(source, fn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pure beauty :) it's worth to add
R
hereselector: (value: T) => Promise<R>|R