How does one make their own asynchronous functions without access to things like Promises or Async/Await? I'm having no trouble finding examples of writing such things using these, but it requires newer versions of Javascript. I'd like to know how you'd write such things without these newer features.
When I would write a function that accepted a callback, for example
let wait5 = function (callback) {
let expire = Date.now()+5000;
while(Date.now() < expire){
}
console.log("Waited 5 seconds!")
if(callback)
callback();
}
wait5(function (){
console.log("Called after waiting 5 seconds")
});
console.log("This should log before 5 seconds passes");
The above does not print log messages in the order I want, instead it blocks on wait5()
until it's done waiting.
Obviously The point is wait5
is meant to emulate a lengthy process, such as sending and receiving over serial or parsing a large amount of data, one that might have a long running loop and no set expected time of completion beyond 'eventually'.
So how would I make this asynchronous without Promises?