0

I saw a pathological code snippet written in LinkedIn so while the example itself doesn't matter, I'd like to understand why the code behaves this way

let run = true;

setTimeout(()=>{run=false;},0);

while (run) {console.log("running...");}

Which prints "running..." repeatedly since run=false is never executed.

But why is this case? The time to execute is set to 0 seconds so why is it not ran as soon as the line is interpreted?

Answers talked about yielding the call stack, the event never running because the current task is still running, etc.. But I'm not familiar with how this actually works.

If I had to guess, my explanation would be setTimeout is an async function that is auto-awaited the next time the current execution polls the other threads but for some reason the while loop never allows this polling to happen

But idk if that's right or if I'm speaking nonsense

financial_physician
  • 1,672
  • 1
  • 14
  • 34
  • No, it's not printing "running". check here: https://stackblitz.com/edit/js-a81fv6?file=index.js – Joel Oct 24 '22 at 07:37
  • [a value of 0 is used, meaning execute "immediately", or more accurately, **the next event cycle**.](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) – Quentin Oct 24 '22 at 07:37
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#run-to-completion – Ivar Oct 24 '22 at 07:38
  • @joel what else is it doing? – kevinSpaceyIsKeyserSöze Oct 24 '22 at 07:45
  • @kevinSpaceyIsKeyserSöze Well, check the stackblitz and look for urself. – Joel Oct 24 '22 at 07:48
  • @Joel [It's running](https://i.stack.imgur.com/gf8hx.png). – Ivar Oct 24 '22 at 07:51
  • 1
    Javascript is single-threaded (aside from workers). Your while loop never allows your time out to run since it's keeping the thread busy forever. Setting the timeout to `0` only means "run it as soon as there are no messages in the queue" (which never occurs). https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop?retiredLocale=de#run-to-completion – connexo Oct 24 '22 at 07:52
  • @Ivar On my end it's not *printing "running..." repeatedly* as OP stated. In fact, it's not printing anything. – Joel Oct 24 '22 at 07:53
  • 1
    It should be. Either you are looking in the wrong place (the console that is provided by Stackblitz doesn't update because it is part of the browser window, and that will only update after the code has run to completion (which it never does)), or the script doesn't load in correctly. – Ivar Oct 24 '22 at 07:55
  • @connexo that makes sense, I think I want to look more into how events get scheduled (is it only on function calls? How would nested functions behave? Etc..) But I can dig deeper myself. Thanks! – financial_physician Oct 24 '22 at 07:58
  • 1
    @Ivar Yeah i'm looking in the console... But you're probably right about it never printing anything due to it's printing after code has completed running. – Joel Oct 24 '22 at 08:01
  • @Joel I don't get it what should I see there except that it's printing "running.."? – kevinSpaceyIsKeyserSöze Oct 24 '22 at 09:16

0 Answers0