I don't agree with the answer given.
Where I do agree is that within the context of JavaScript, "zombie processes" don’t exist in the same way they do in lower-level languages like C++ or environments like .NET, where an actual OS-level process or thread can become orphaned and continue running indefinitely. It is also unfair to compare Javascript with a language like C++ or .Net.
Given the OP's example, zombie-like behaviors can occur in JavaScript if you define them as tasks or functions continuing to execute even after their results are no longer needed.
I can come up with several techniques to create "zombie"-like behaviours:
- You will never be able to stop this interval if you don't own the interval id.
let counter = 0; setInterval(() => { console.log(`c: ${++counter}`); }, 1000);
- An asynchronous operation never cancelled.
(async () => { while (true) { console.log('fetching data...'); // simulate an endless task await new Promise((r) => setTimeout(r, 1000)); } })();
- Overlapping retries
- Event listeners not being removed
- Deferred promises without resolution
const deferred = new Promise(() => { // never resolve or reject }); // this will never execute deferred.then(() => console.log('resolved!'));
- And the simplest, endless loop in an IIFE
(() => { while(true) { // you will never (ever) be able to interrupt this function. console.log('I never stop'); } })();
Because Javascript lacks the implementation of interrupting a function (or the execution of it), "zombie" functions/processes/whatever, are quite easily made and is part of the web since Javascript became mainstream.
Workers (ServiceWorkers, WebWorkers, Worklets) try to simulate behavior common to other languages, but it comes with tremendous limitations.
It is simply the lack of implementation Javascript offers to stop a function from execution, the following is wishful thinking:
const fn = () => { while(true) { console.log('I never stop'); } }; // how nice it would be to receive some sort of an id. const functionId = Function.call(fn); stopFunction(functionId); // or a proxy to signal the function to stop const proxy = Function.call(fn); proxy.interrupt();
But, reality is a b...h ;)
windowis not necessarily the name of the 'top level object' . for instance in Node.js it is namedglobal