So, I am making a fake operating system game inside the console in JS. And to load the OS I have a twirly loading function which I'll show now.
function OSLoad(OS = "", loadingTimeSeconds = 10) {
var twirlTimer = (function () {
var P = ["\\", "|", "/", "-"];
var x = 0;
return setInterval(function () {
process.stdout.write(
"\r" + P[x++] + " Loading " + OS
);
x &= 3;
}, 250);
})();
setTimeout(() => clearInterval(twirlTimer), loadingTimeSeconds + "000");
}
I'm kind of a beginner, and I've ran into a problem with this function. I'm currently in the testing phase, so after this function is done I log to the console "OS Started!", but instead it logs the loading and "OS Started!" at the same time. I assumed that setTimeout would make it pause until it's done, which it didn't. What can I do to solve this?
The first thing that came to mind when I tried to solve this was using async, but as I said, I'm a beginner, and I never actually learned how to use async... So if you could teach me how to use that or an even better solution please do.