Skip to content

Add utility to synchronously inspect promise state #40054

@sindresorhus

Description

@sindresorhus

Is your feature request related to a problem? Please describe.

In most cases, you should just await the promise and get its value, even if the promise has already resolved. However, it could sometimes be useful to get the state of the promise. For example, as an optimization to only do a heavy operation if the promise in a certain state or for assertions when writing tests.

Describe the solution you'd like

There used to be process.binding('util').getPromiseDetails(promise), but it was removed in Node.js 16. I suggest exposing a util.promiseState(promise) method which would return 'pending' | 'fulfilled' | 'rejected' depending on the promise state.

Describe alternatives you've considered

I have made a package for this, but it depends on parsing util.inspect() output which is a bit fragile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions