Created
May 30, 2017 18:58
-
-
Save mattpodwysocki/88ce9191603a4a568caf4f8474ded35e 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>; | |
private _selector: (value: T) => Promise<R>; | |
constructor(source: Iterable<T>, selector: (value: T) => Promise<R>) { | |
this._source = source; | |
this._selector = selector; | |
} | |
async *[Symbol.asyncIterator]() { | |
for (let item of this._source) { | |
let result = await this._selector(item); | |
yield result; | |
} | |
} | |
} | |
export function mapAsync<T, R>( | |
source: Iterable<T>, | |
selector: (value: T) => Promise<R>, | |
thisArg?: any): AsyncIterable<R> { | |
const fn = selector.bind(thisArg); | |
return new MapAsyncIterable<T, R>(source, fn); | |
} |
rbuckton
commented
May 30, 2017
And why worry about thisArg
bindings in a world with arrow functions?
See https://gist.github.com/rbuckton/c60954f23effa3992f9151d72ba7431d for what I'd recommend.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment