Functions in pure JavaScript are synchronous by default. Is there a way to convert synchronous functions to become asynchronous.
Note: I don't want to use setTimeout function (Or setImmediate In Node.js). Just ECMAScript specs. Tried to add async keyword but the result is the same synchronous behavior.
The Example below illustrates the default behavior.
Code:
console.log("start");
function countTo100Mill() {
for(i=0; i <= 100000000; i++) {
if(i === 100000000) {
console.log("count to 100mill complete");
}
}
}
function countTo100() {
for(i=0; i <= 100; i++) {
if(i === 100) {
console.log("count to 100 complete");
}
}
}
countTo100Mill();
countTo100();
console.log("end");
Default Behaviour:
The function countTo100Mill
will run before countTo100
no matter the delay.
What I Want To Achieve:
I want a way to convert the functions defined in the code above to run asynchronously so that countTo100
will complete followed by countTo100Mill
.