title: Call endpoint within sveltekit beforeNavigate function and cancel navigation based on response
body: <p>I want to call a route verification endpoint inside sveltekit's <code>beforeNavigate</code> method and prevent moving to the new route based on that endpoint response status.</p>
  <p>I'm unable to cancel routing using <code>cancel()</code> after make an endpoint call. It will not work if used after the call, right where I get the response.</p>
  <p>What am I missing here?</p>
  <p>This is what i have:</p>
  <pre class="lang-js prettyprint-override"><code>// __layout.svelte
  beforeNavigate(async ({ to, from, cancel }) =&gt; {
      // &gt; cancel() will work if here
  
      const url = to || from;
  
      const path = url ? url.pathname.slice(1, url.pathname.length) : null;
  
      // cancel() will work if here
      
      try {
  
         // &gt; cancel() will work if here
  
         const response = await fetch(`/api/check?path=${path}`, {
            method: 'GET',
         });
  
         // &gt; cancel() will not work here
  
         if ([401, 403].includes(response?.status)) {
            displayNotification('Not authorized');
            cancel(); // not working
         }
      } catch (error) {
         displayNotification('Something went wrong!');
         cancel(); // not working
      }
  });
  </code></pre>