Soumya Sagnik Khanda
Soumya Sagnik Khanda

Reputation: 27

Behavior of Global Execution Context wrt setTimeout

Let's say I have the following piece of code

function perpetuity() {
  console.log("Being called");
  setTimeout(perpetuity, 1500);
}

perpetuity();

I would like to know if the global execution context is present in the call stack so that this code can be executed. OR If the global execution context is created and deleted along with the callback function's execution context every 1500ms.

Chat gpt says GEC never stops but I cannot find the GEC in my browser's call stack when executing this.

Upvotes: 0

Views: 128

Answers (1)

Bergi
Bergi

Reputation: 664375

There is no "global execution context". (I assume you're not confusing this with the global environment record?)

There is however a realm execution context for all js code running in a browser. It is not actually useful for anything and more of a technicality (to allow tracking the realm that caused the execution), but it is pushed to the execution context stack before running any JS code and popped afterwards. It is created in InitializeHostDefinedRealm together with the intrinsics, the global object and the global environment of the realm, to create the realm.

Upvotes: 1

Related Questions