Skip to content

Instantly share code, notes, and snippets.

@xwlee
Created June 7, 2023 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xwlee/9bf3fdcd6283787e5b3cf0285b0f9e03 to your computer and use it in GitHub Desktop.
Save xwlee/9bf3fdcd6283787e5b3cf0285b0f9e03 to your computer and use it in GitHub Desktop.
mapAsync.ts
const mapAsync = <T, R>(arr: T[], fn: (x: T) => Promise<R>) =>
Promise.all(arr.map(fn));
(async () => {
console.log("START MAP");
const mapped = await mapAsync([1, 2, 3, 4], async (n) => {
const x = await fakeAPI(n * 1000, n);
return x * 10;
});
useResult(mapped);
console.log("END MAP");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment