Message from JavaScript discussions
July 2017
— But seriously, wait till you see how that function is used, it will make your head spin
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
— 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));
}
}
— *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));
— saveInsertionThunk
is a function which calls conf.cache.saveDoc(templLoc, templDoc);
before calling insertionThunk