It's because it is using
yield(). This returns out of the current function, returning control to the program. That way everything else can execute. It only returns to the code once the signal or
resume() is applied. Then it carries on where it left off.
In this example
idle_frame is a signal that fires after a game frame has passed.
The first example your code is trapped in that loop and locks the program.
In the second example, the yield is going to yield for one frame. Allowing the program to continue. Then once the idle_frame is signaled, the code will resume at that point, go up into the while loop, and do it again, yield again. So your infinite loop should only execute once per frame.
However, nothing below that
while true: will ever execute, because the function is trapped in that loop.