Skip to content

Instantly share code, notes, and snippets.

@jbutko
Created January 23, 2019 10:23
Show Gist options
  • Save jbutko/674a714525245e8b49db7ca14156f898 to your computer and use it in GitHub Desktop.
Save jbutko/674a714525245e8b49db7ca14156f898 to your computer and use it in GitHub Desktop.
Async/Await examples

serial processing

async function printFiles () {
  const files = await getFilePaths();

  for (const file of files) {
    const contents = await fs.readFile(file, 'utf8');
    console.log(contents);
  }
}
// via https://stackoverflow.com/a/37576787/3151019

parallel (concurent) processing

  async function parallel(jobs) {
    const promises = jobs.map((job) => doJob(job, job));
    const results = await Promise.all(promises)
    let finalResult = 0;
    for (const result of results) {
     console.log(result);
     ++finalResult;
    }

    return finalResult;
  }
// via https://techbrij.com/javascript-async-await-parallel-sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment