react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Clarification on new willTransitionTo callback

Open rickharrison opened this issue 11 years ago • 14 comments

I am upgrading to 0.12 and I am slightly confused about how to handle the upgrade from wait to callback. If I call transition.redirect during some asychronous behavior, do I need to call the callback or should I not? Here is a sample of what I am trying to upgrade:

authPromise.then(function () {
  if (!someCondition) {
    transition.redirect('other-route');
  }
}).catch(function (err) {
  SignIn.attemptedTransition = transition;

  transition.redirect('sign-in');
});

transition.wait(authPromise);

Do I need to call the callback in the catch of the promise? In the then handler should I call the callback in an else block of the if (!someCondition) or can I just call it all the time in that then handler?

rickharrison avatar Feb 26 '15 22:02 rickharrison

You must call the callback if you define this parameter (which you need to define for “waiting”). Just chain it as last step:

authPromise.then(function () {
  if (!someCondition) {
    transition.redirect('other-route');
  }
}).catch(function (err) {
  SignIn.attemptedTransition = transition;

  transition.redirect('sign-in');
}).then(callback);

(You can also do .then(callback, callback) if you want navigation to occur regardless of failures in your code.)

If you're writing only sync code, you can omit callback in parameter list and router will call it for you.

gaearon avatar Feb 26 '15 22:02 gaearon

So just to clarify, even if I redirect the transition away using transition.redirect, the callback should still be called.

rickharrison avatar Feb 26 '15 23:02 rickharrison

Yes. It would be more error-prone if we were more forgiving and interpreted transition methods as callback calls.

gaearon avatar Feb 27 '15 14:02 gaearon

Closing as this appears to be resolved.

gaearon avatar Mar 04 '15 13:03 gaearon

@gaearon I just realized that this ends up swallowing any errors that I have in my render methods of components that use this mixin. Do you have any suggestions about how to handle this?

rickharrison avatar May 03 '15 22:05 rickharrison

I think you should be able to rethrow?

.catch(function (err) {
  SignIn.attemptedTransition = transition;
  transition.redirect('sign-in');
  throw err;
});

gaearon avatar May 03 '15 22:05 gaearon

I tried this, and nothing appeared in the console:

.catch(function (err) {
  SignIn.attemptedTransition = transition;
  transition.redirect('sign-in');
}).then(callback).catch(function (err) {
  throw err;
});

However, if I console.log in that last catch it exists, but for some reason throw is not doing anything.

rickharrison avatar May 03 '15 23:05 rickharrison

Depending on the promise library you're using (like es6-promise) a thrown error that isnt handled doesnt bubble up to window. I ended up switching to Q because of this.

merk avatar May 04 '15 03:05 merk

Yea, I am using es6-promise so that explains why nothing was happening. There has to be some way to surface the error without every single error in render being swallowed.

rickharrison avatar May 04 '15 19:05 rickharrison

You can use setTimeout:

.catch(function (err) {
  setTimeout(function(){
    throw err;
  }, 0);
});

Or simply:

.catch(function (err) {
  SignIn.attemptedTransition = transition;
  transition.redirect('sign-in');
}).then(function(){
  setTimeout(function(){
    callback();
  }, 0);
});

Some libraries also provide a promise.done() for cases like this, but it's not part of the spec.

agundermann avatar May 04 '15 20:05 agundermann

Seems so janky, but also will fix the issue. Thanks for the tip. This is why some days I just really don't like promises haha.

rickharrison avatar May 04 '15 20:05 rickharrison

I tried this, and nothing appeared in the console:

This is not exactly what I wrote. You're rethrowing after first catch. This won't work because you're swallowing the error in the first catch. You need to rethrow there.

gaearon avatar May 04 '15 23:05 gaearon

My original code wasn't right either; it seems that you want to fire callback before rethrowing. Hmm. What is that error you're catching? Can you check its type in catch and only rethrow unfamiliar errors?

gaearon avatar May 04 '15 23:05 gaearon

In development, I had a SyntaxError in one of my render functions. What happened was that the error was being swallowed by the .then(callback). It resulted in a completely white page and nothing logged out to the console.

If I place another catch after the transition callback the error exists in that catch when I use console.log. I've verified that the error exists, but throwing it does nothing. It seems that the reason is due to how es6-promise behaves as merk described above.

To clarify the process looks like this:

transition call's callback -> Route render() -> error in render() -> swallowed

rickharrison avatar May 04 '15 23:05 rickharrison