Skip to content

Commit

Permalink
refactor: await-in-loop issue 11ty#1
Browse files Browse the repository at this point in the history
  • Loading branch information
franktip committed Aug 23, 2021
1 parent 6776e87 commit 31f913c
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/TemplateLayout.js
Expand Up @@ -119,13 +119,24 @@ class TemplateLayout extends TemplateContent {
async getCompiledLayoutFunctions(layoutMap) {
let fns = [];
try {
for (let layoutEntry of layoutMap) {
fns.push(
await layoutEntry.template.compile(
await layoutEntry.template.getPreRender()
)
);
}
// DR-ASYNC REFACTOR AWAIT-IN-LOOP
// for (let layoutEntry of layoutMap) { //console.log("*** EXECUTING awaitInLoop /src/TemplateLayout.js:122:128");
// fns.push(
// await layoutEntry.template.compile(
// await layoutEntry.template.getPreRender()
// )
// );
// }

let preRenders = await Promise.all(
layoutMap.map((layoutEntry) => layoutEntry.template.getPreRender())
);
let compiled = await Promise.all(
layoutMap.map((layoutEntry, index) =>
layoutEntry.template.compile(preRenders[index])
)
);
layoutMap.forEach((_, index) => fns.push(compiled[index]));
} catch (e) {
debugDev("Clearing TemplateCache after error.");
templateCache.clear();
Expand Down Expand Up @@ -157,6 +168,7 @@ class TemplateLayout extends TemplateContent {
let layoutMap = await this.getTemplateLayoutMap();
let fns = await this.getCompiledLayoutFunctions(layoutMap);
for (let fn of fns) {
//console.log("*** EXECUTING: 4: awaitInLoop /src/TemplateLayout.js:159:162");
templateContent = await fn(data);
data = TemplateLayout.augmentDataWithContent(data, templateContent);
}
Expand Down

0 comments on commit 31f913c

Please sign in to comment.