How can I implement deffered deeplinking in Javascript, given I have no idea how’s it done in flutter?

Flutter devs in my team have implemented a traditional deep linking mechanism that shows “continue with app or chrome option” when the app is already installed, otherwise nothing happens. I tried implementing deferred deep linking as shown in the below code as an example, but I need some more reliable implementation.

window.location.href = 'example_app://';

// fire a function if above redirection fails
setTimeout(function() {                
  window.location.href = "https://play.google.com/store/apps/details? 
    id=<app_id>;
}, 5000);

The problem with this is even though I expected href to stop JS execution of the previous page, it doesn’t, and hence code becomes unpredictable.
Is there a way where I can implement deferred deep linking without branch.io or other third-party implementation? And why can it be better to use third-party implementation rather than something made from scratch?