Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created May 30, 2017 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattpodwysocki/88ce9191603a4a568caf4f8474ded35e to your computer and use it in GitHub Desktop.
Save mattpodwysocki/88ce9191603a4a568caf4f8474ded35e to your computer and use it in GitHub Desktop.
'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
Copy link

export function mapAsync<T, R>(
  source: Iterable<T | PromiseLike<T>> | AsyncIterable<T>,
  selector: (this: undefined, value: T) => R | PromiseLike<R>,
  thisArg?: undefined): AsyncIterable<R>;

export function mapAsync<T, R, This>(
  source: Iterable<T | PromiseLike<T>> | AsyncIterable<T>,
  selector: (this: This, value: T) => R | PromiseLike<R>,
  thisArg: This): AsyncIterable<R>;

@rbuckton
Copy link

And why worry about thisArg bindings in a world with arrow functions?

@rbuckton
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment