When browser window is closed all resources acquired by the software is released instantly. So when I close the window, logically there should not be any process running. So how exactly is the function bound to beforeunload executes. Moreover what if there is an AJAX call to server, how does it guarantee that the server database will write my contents that I am passing through this function.
window.addEventListener("beforeunload", function (e) {
// AJAX CALLto the server
(e || window.event).returnValue = null;
return null;
});
How is that AJAX call executed. My browser gets closed in not even a second. What if I want to write to the database, will this AJAX call execute successfully if yes then how is it implemented internally.
EDIT : Someone in comments pointed out that there is no guarantee of that AJAX call. So that means there is a chance. If there is a chance then how that process is executed. And also what happens if we close the tab. Is it still 100% guaranteed. If yes then why and how?