Skip to content

act should yield the value returned from the async function. #16366

Closed
@pfgray

Description

@pfgray

Do you want to request a feature or report a bug?
Feature

A couple of things have surprised me about the new asynchronous act API.

1. Act doesn't return a promise.

Currently the act function returns an object with a then method that doesn't return a promise.

Recently, I tried to write something like:

act(() => {
  return doSomethingAsynchronous()
})
.then(() => {
  // ...
})
.then(() => {
  // ...
})

The second .then call throws an error because the value that is returned from act isn't a full-fledged Promise, but rather just an object that contains a .then method that returns undefined. This is fine for simple async/await cases, but definitely caught me by surprise.

2. Act discards the returned value.

Act also throws away the value returned from the async function callback, which makes it harder to use the values generated there. For example, if I wanted to use the value yielded by doSomethingAsynchronous, I would need to introduce a mutable variable:

let returnedValue;
await act(async () => {
  returnedValue = await doSomethingAsynchronous()
})

// use returnedValue here

If act simply propogates the returned value from the asynnchronous callback, this will solve the confusing bits above, allowing for things like:

const returnedValue = await act(() => doSomethingAsynchronous())

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