Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect and warn if use(promise) is wrapped with try/catch block #25543

Merged
merged 1 commit into from
Oct 28, 2022

Commits on Oct 28, 2022

  1. Throw custom error object to suspend

    The old (unstable) mechanism for suspending was to throw a promise. The
    purpose of throwing is to interrupt the component's execution, and also
    to signal to React that the interruption was caused by Suspense as
    opposed to some other error.
    
    A flaw is that throwing is meant to be an implementation details — if
    code in userspace catches the promise, it can lead to
    unexpected behavior.
    
    With `use`, userspace code does not throw promises directly, but `use`
    itself still needs to throw something to interrupt the component and
    unwind the stack.
    
    The solution is to throw an internal error. In development, we can
    detect whether the error was caught by a userspace try/catch block and
    log a warning — though it's not foolproof, since a clever user could
    catch the object and rethrow it later.
    
    The error message includes advice to move `use` outside of the
    try/catch block.
    
    I did not yet implement the warning in Flight.
    acdlite committed Oct 28, 2022
    Configuration menu
    Copy the full SHA
    da38d04 View commit details
    Browse the repository at this point in the history