Last active
May 31, 2021 23:18
-
-
Save lukehoban/0f366c5fd1af307b87da to your computer and use it in GitHub Desktop.
Revisions
-
lukehoban revised this gist
Apr 9, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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 = {}; -
lukehoban revised this gist
Apr 9, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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 = {}; -
lukehoban created this gist
Apr 9, 2015 .There are no files selected for viewing
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 charactersOriginal 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; }