'conditional in beforeunload based on user's choice to leave or stay

Is there any way to execute different functions in a beforeunload event listener, based on whether the user chooses to leave the page or not?

window.addEventListener('beforeunload', (e) => {
  // if (user chooses to leave) 
    func1(); // a value is sent to database
  // else if (user chooses to stay on the page)
    func2(); // actually nothing happens
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source