Skip to content

Suspense does not suspend on first render #242

Open
@hrougier

Description

@hrougier

Hi,

It looks like there is an issue with the current suspense implementation.

Considering this snippet

const { data } = useAsync({
  suspense: true,
  promise: new Promise(resolve => {
    setTimeout(() => resolve('hello'), 1000)
  }),
})

// we should be able to safely use data here as this line
// should not be reached until promise has resolved
console.log('data', data)

But we get the following output

data undefined
data hello

One of the most important purpose of suspense is to make asynchronous code feel like if it were synchronous so you don't have to deal with "data is not here yet" headaches.

By quickly looking at the implementation, this may be due to this condition

if (options.suspense && state.isPending && lastPromise.current !== neverSettle) {
  // Rely on Suspense to handle the loading state
  throw lastPromise.current
}

If lastPromise.current equals neverSettle during the first render, then it won't throw and the first render will not be suspended.

Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions