Use async/await syntax in _updateQa

This commit is contained in:
Henrik Giesel 2020-12-29 13:32:04 +01:00
parent db3308e788
commit ecdb06cbd6

View file

@ -21,7 +21,7 @@ function _runHook(arr: () => Promise<any>[]): Promise<any[]> {
return Promise.all(promises); return Promise.all(promises);
} }
function _updateQA(html, fadeTime, onupdate, onshown) { async function _updateQA(html, fadeTime, onupdate, onshown) {
// if a request to update q/a comes in before the previous content // if a request to update q/a comes in before the previous content
// has been loaded, wait a while and try again // has been loaded, wait a while and try again
if (_updatingQA) { if (_updatingQA) {
@ -39,9 +39,9 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
var qa = $("#qa"); var qa = $("#qa");
// fade out current text // fade out current text
new Promise((resolve) => qa.fadeTo(fadeTime, 0, () => resolve())) await qa.fadeTo(fadeTime, 0).promise();
// update text // update text
.then(() => {
try { try {
qa.html(html); qa.html(html);
} catch (err) { } catch (err) {
@ -51,23 +51,23 @@ function _updateQA(html, fadeTime, onupdate, onshown) {
String(err.stack).substring(0, 2000) String(err.stack).substring(0, 2000)
).replace(/\n/g, "<br />") ).replace(/\n/g, "<br />")
); );
} };
}) await _runHook(onUpdateHook);
.then(() => _runHook(onUpdateHook))
.then(() =>
// @ts-ignore wait for mathjax to ready // @ts-ignore wait for mathjax to ready
MathJax.startup.promise.then(() => { await MathJax.startup.promise.then(() => {
// @ts-ignore clear MathJax buffer // @ts-ignore clear MathJax buffer
MathJax.typesetClear(); MathJax.typesetClear();
// @ts-ignore typeset // @ts-ignore typeset
return MathJax.typesetPromise(qa.slice(0, 1)); return MathJax.typesetPromise(qa.slice(0, 1));
}) });
)
// and reveal when processing is done // and reveal when processing is done
.then(() => new Promise((resolve) => qa.fadeTo(fadeTime, 1, () => resolve()))) await qa.fadeTo(fadeTime, 1).promise();
.then(() => _runHook(onShownHook)) await _runHook(onShownHook);
.then(() => (_updatingQA = false));
_updatingQA = false;
} }
function _showQuestion(q, bodyclass) { function _showQuestion(q, bodyclass) {