Created
June 28, 2017 07:28
-
-
Save saiberz/3c4607cfd2bf66c4f12e51a124d2ce07 to your computer and use it in GitHub Desktop.
await-async-map
This file contains hidden or 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
// 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