Message from JavaScript discussions

July 2017

— There is this multi-headed medusa function which sits right at the top of the sequence when a user calls loadTemplate, it branches SO MUCH it makes my head hurt

Message permanent page

— 

So I put that in a function inside that function, because all the branches have to perform that operation but in different ways

— Uhhhh

— 

if (cachedDoc !== null) {

  // Synchronous

  return insertionThunk(cachedDoc);

} else {

  if (conf.isFile) {

  // Asynchronous XHR

  return getTemplateXHR(templLoc, conf).then(saveInsertionThunk);

  } else {

    // Synchronous

    return saveInsertionThunk(getTemplateDOM(templLoc));

  }

}

Message permanent page

— *ahem*

— 

if (x) {
return y;
} else {
// rest of logic
}

— You see a problem here?

— What you see is a little cache check

— If we don't have a cached document, we go get it in the else

— 

if (cachedDoc !== null) {
return insertionThunk(cachedDoc);
}
if (conf.isFile) {
return getTemplateXHR(templLoc, conf).then(saveInsertionThunk);
}
return saveInsertionThunk(getTemplateDOM(templLoc));

Message permanent page

— saveInsertionThunk is a function which calls conf.cache.saveDoc(templLoc, templDoc); before calling insertionThunk

Message permanent page

— Equivalent