JavaScript · Senior · Question 59 of 75

Why can this code freeze the page even though it uses promises?

function loop() { Promise.resolve().then(loop); }
loop();

Explanation

The browser renders only between macrotasks after the microtask queue drains. Use setTimeout, MessageChannel or scheduler.yield() to give rendering a chance.

Further reading

Problem with a question

JavaScript · Senior · question 59

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz