Skip to content

Instantly share code, notes, and snippets.

@saiberz
Created June 28, 2017 07:28
Show Gist options
  • Save saiberz/3c4607cfd2bf66c4f12e51a124d2ce07 to your computer and use it in GitHub Desktop.
Save saiberz/3c4607cfd2bf66c4f12e51a124d2ce07 to your computer and use it in GitHub Desktop.
await-async-map
// node --harmony await-async-map.js
async function map([head, ...rest], fn, acc = []) {
if (!head) {
return acc;
}
return map(rest, fn, [...acc, await fn(head)]);
};
// example
const movies = ['lost in translation', 'donnie darko'];
const search =
movie => new Promise(
resolve => setTimeout(() => resolve(movie.toUpperCase()), 1000)
);
async function main() {
const result = await map(movies, movie => search(movie));
console.log(result);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment