async function() {
while (true) {
await null;
}
}
This will block the thread forever, because the event loop would try to finish all microtasks in one frame.
But if the event loop suspends processing next microtask after 1 second, the thread can be used to process other code, for example, animation frame.
Could it be possible for a JavaScript engine to detect potentially bad code?
Yes!
Is this useful?
Certainly could be. After all, TypeScript's and (to some extent) ESLint's job is to find problematic code and warn you.
Does the engine warn against potentially code?
Sometimes! For instance, most engines will warn you when rejected Promises remain uncaught.
Why don't they for this case?
I can't speak for JavaScript engine manufacturers, but I can potentially imagine that if they would implement a warning / some behavior against your pet issue is going to be based on a number of factors, including how likely it is that this is a problem, how easy it is to detect and what's the cost in the engine to try and figure out if something bad is happening. Nothing is free and JavaScript engines need to be fast.
In my opinion this is an unlikely mistake for someone to make, and if the mistake is made it's quickly noticed because of how bad the effects are. This doesn't feel like the case where handholding is needed.
But even if this is a common problem, I feel ESLint is probably still a better place for something to detect and warn you.