I have a function which is doing a heavy job so I decided to run this function asynchronously and prevent browser freezing when my function is running however after I add promise to my function nothing happened and browser still freeze until function works done. so I simulate my function heavy job with a simple for loop and add some extra useless code inside that for loop in order to prevent javascript engine skip for loop.so after I run the code it turns out for loop doesn't run asynchronously so what is wrong here?
here is sample code simulate my problem:
new Promise(resolve =>{
//some heavy task
for(let i=0 ; i<10000; i++){
if(i % 2 == 0){
console.log(i);
}
}
resolve();
}).then(()=>{
console.log("finished")
})
console.log("javascript engine should continue running");
So in my idea first the last line console log must runs because javascript should push that promise into event queue and after all that logs inside that for loop printed finally finished should be printed however it isn't the case.