I have a code block something like this:
function printData(response) {
response.json()
.then(data => console.log(data.name));
}
const fetchedData = fetch("http://localhost:3000/url");
fetchedData.then(printData);
console.log("First console output");
This code executes directly whenever the page loads. My endpoint fetch("http://localhost:3000/url")
returns response after 200ms.
Since the XHR will resolve after all global execution context finishes, it should get out of the call stack. But does global execution really gets out of the call stack? Because there is still a task to process, still we need to run printData
function after request resolves. Will global execution context wait until request resolves or does it get out of the call stack even there are some requests still being fetched?
Flow is like this;
- printData set to memory
- fetchedData variable set to memory and browser feature xhr initiates the request.
- printData assigned to fetchedData promise.
- console output.
Now global execution context finishes but still, we are waiting for the response of the fetch("http://localhost:3000/url");. Will the global execution context get out of the call stack or will it wait the request to fulfill?