Skip to content

Instantly share code, notes, and snippets.

@gognjanovski
Created March 24, 2021 12:12
Show Gist options
  • Save gognjanovski/b3a59f933b110fb15664d82caedb6981 to your computer and use it in GitHub Desktop.
Save gognjanovski/b3a59f933b110fb15664d82caedb6981 to your computer and use it in GitHub Desktop.
Asynchronous Map function extension for the Array interface
interface Array<T> {
mapAsync<U>(callbackfn: (value: T, index: number, array: T[]) => Promise<U>, thisArg?: any): Promise<U[]>;
}
Array.prototype.mapAsync = function<U>(callbackfn: (value: any, index: number, array: any[]) => Promise<U>, thisArg?: any): Promise<U[]> {
var promises = this.map((value: any, index: number, array: any[]) => callbackfn(value, index, array));
return Promise.all(promises);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment