UPDATE: I think the question I have regarding async/await is a little more nuanced than the linked suggestion and have updated the title of the question to hopefully better reflect this. I'm specifically concerned with how to start some asynchronous work that potentially takes a while --but does not impact the majority of a page's logic which needs to happen in sequence-- and then wait for the asynchronous work to complete before moving forward with a subset of the page's logic that does rely upon the asynchronous logic. The answer winds up being essentially the same (await Promise.all
), but the clarity provided by the accepted answer for my question was more valuable to me than the linked answer (abstract the sequential logic into its own asynchronous function before using await Promise.all
).
I'm having trouble finding out if this is possible in javascript (the comment block marked with "XXX" at the beginning is the code with which I'm struggling (pretend that tasks 1-4 must be executed in order and cannot be parallelized)):
document.addEventListener('DOMContentLoaded', () => {
// Kick off some asynchronous piece of work that potentially takes some
// time (for instance, opening and upgrading an indexedDB) but that should
// not hold up other work that should happen in the meantime (ie: executing
// tasks 1-3).
asynchronousSetup(); // 1000 - 1500 ms of work
// Do a bunch of other stuff that should happen in sequence and as quickly
// as possible (to enable meaningful user interaction in the client).
executeTask1(); // 300 - 400 ms of work
executeTask2(); // 300 - 400 ms of work
executeTask3(); // 300 - 400 ms of work
// XXX Wait for completion of asynchronousSetup() before proceeding with
// any additional work that is also necessary for meaningful user
// interaction in the client, but that requires that the work done in
// asynchronousSetup() is completely finished.
if (/* asynchronousSetup() complete */) {
executeTask4(); // Relies on work done in tasks 1-3 and asynchronousSetup()
}
});
I'm familiar with async/await and promises in javascript, but I haven't seen any demonstrations of their ability to accomplish this sort of thing without --what feels to me like a code smell-- setting an interval or timeout to check for the initialization of some common variable at the point where I want to ensure asynchronousSetup()
has completed before firing executeTask4()
.
It would be really sweet if I could do:
// ...Same initial code as above.
const initialized = asynchronousSetup();
// ...Same intervening code as above.
if (await initialized) {
executeTask4();
}
assuming that asynchronousSetup()
was appropriately decorated with async
:
async function asynchronousSetup() {
// Logic of asynchronousSetup()
}
but I've tried that before with no success.
Thanks, and sorry if this is an obvious one; I haven't had any luck in my searches or my experiments in code. I have a feeling I'm going to feel very dumb once someone points out how to achieve this... but I'll take my knocks; this feels like a substantial hurdle in my understanding that I need to overcome before I can write performant javascript that results in good UX ;p