I know that when there is a CPU intensive code any immediate previous DOM update won't happen. Such as
function blockFor(dur){
var now = new Date().getTime();
while (new Date().getTime() < now + dur);
result.textContent = "I am done..!";
}
result.textContent = "Please remain..."; // we will never see this
blockFor(2000);
<p id="result"></p>
However if I shift the CPU intensive code to the asynchronous timeline by setTimeout
it's all fine as in the following snippet.
function blockFor(dur){
var now = new Date().getTime();
while (new Date().getTime() < now + dur);
result.textContent = "I am done..!";
}
result.textContent = "Please remain..."; // now you see me
setTimeout(_ => blockFor(2000),15); // 15ms to be on the safe side
<p id="result"></p>
However since i know that promises also take you to a "sort of" asycnronous timeline i was expecting to achieve the same effect without using the setTimeout
hack. Such as;
function blockFor(dur){
var now = new Date().getTime();
while (new Date().getTime() < now + dur);
result.textContent = "I am done..!";
}
result.textContent = "Please remain..."; // not in Chrome not in FF
Promise.resolve(2000)
.then(blockFor)
<p id="result"></p>
I would at least expect this to run as expected in FF because of this perfect article (https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/) alas no way.
Is there any way to accomplish this job with promises?