Better know a framework #10 cancelling navigation on timeout

This one is awesome.
In discussion to one of the issues Navigation pipeline should support cancelation

@davismj shared another good usage of promises:

activate() {
  return new Promise((resolve, reject) => {
    let fail =  setTimeout(reject, 15000);   // not good for SSR
    this.doMyReallyLongTimeThing().then(()=>{
    clearTimeout(fail)
    resolve()
   });
  }
} 

Dead simple and to the point.
Either we get there in 15 seconds or bail out.
Way cleaner then modifying router pipeline I bet.

2 Likes

Keep in mind these things might be troublesome with SSR and the use of settimeout. Besides that if the async action is successful in time we should clear the timeout

2 Likes

Thanks. Updated a bit the sample.

This is a good use case but what I see regularly is it throws up the rubbish about “a promise was reject with a non-error” into the console often just navigating… it appears to load successfully so it’s not clear why it would throw a warning.

Edit: Seems like a problem with the redirect instructions in the router, the route it’s redirecting to looks wrong.

Promise spec requires to reject with ErrorSomething if not - it yells…
redirection on routes is made with rejecting promises but with other result
there is a discussion in issues for router (i think)

of course spec changed after implementing that (-: