Skip to content

Instantly share code, notes, and snippets.

@lukehoban
Last active May 31, 2021 23:18
Show Gist options
  • Select an option

  • Save lukehoban/0f366c5fd1af307b87da to your computer and use it in GitHub Desktop.

Select an option

Save lukehoban/0f366c5fd1af307b87da to your computer and use it in GitHub Desktop.

Revisions

  1. lukehoban revised this gist Apr 9, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions asyncloops.js
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,7 @@ function fetchData(routes, params) {

    // ES7 with async/await and Promise.all
    // Note: Starting to look a lot more like `normal` procedural code
    // * But there's that one crazy line of "magic" for the parallel loop
    async function fetchData(routes, params) {
    let data = {};

  2. lukehoban revised this gist Apr 9, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions asyncloops.js
    Original file line number Diff line number Diff line change
    @@ -45,6 +45,7 @@ async function fetchData(routes, params) {
    // Note: This actually makes things a lot simpler:
    // * No need for a nested arrow function
    // * Looks just like the procedural equivalent again
    // * The downside is that it may not be obvious that the loop body executes non-sequentially
    async function fetchData(routes, params) {
    let data = {};

  3. lukehoban created this gist Apr 9, 2015.
    57 changes: 57 additions & 0 deletions asyncloops.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    // ES6 w/ Promises
    // Note: From a React starter template - see https://t.co/wkStq8y3I5
    function fetchData(routes, params) {
    let data = {};

    return Promise.all(routes
    .filter(route => route.handler.fetchData)
    .map(route => {
    return route.handler.fetchData(params).then(resp => {
    data[route.name] = resp;
    })
    })
    ).then(() => data);
    }

    // ES7 with async/await and Promise.all
    // Note: Starting to look a lot more like `normal` procedural code
    async function fetchData(routes, params) {
    let data = {};

    await Promise.all(routes.map(async route => {
    if(!route.handler.fetchData) return;
    data[route.name] = await route.handler.fetchData(params);
    }));

    return data;
    }

    // ES? with async/await and hypothetical await*
    // Note: This didn't help much.
    // * Removed 12 characters
    // * But complexity of having nested arrow function and map vs. for..of still there
    async function fetchData(routes, params) {
    let data = {};

    await* routes.map(async route => {
    if(!route.handler.fetchData) return;
    data[route.name] = await route.handler.fetchData(params);
    });

    return data;
    }

    // ES?? with async/await and `asyncparallelfor..of
    // Note: This actually makes things a lot simpler:
    // * No need for a nested arrow function
    // * Looks just like the procedural equivalent again
    async function fetchData(routes, params) {
    let data = {};

    asyncparallelfor(route of routes) {
    if(!route.handler.fetchData) return;
    data[route.name] = await route.handler.fetchData(params);
    };

    return data;
    }