Inspect the state of a promise
You would usually not need this as you can just await the promise at any time to get its value even after it's resolved. This package could be useful if you need to check the state of the promise before doing a heavy operation or for assertions when writing tests.
Vote up this issue if you want to see this feature being included in Node.js itself.
npm install p-state
import timers from 'node:timers/promises';
import {promiseStateSync} from 'p-state';
const timeoutPromise = timers.setTimeout(100);
console.log(promiseStateSync(timeoutPromise));
//=> 'pending'
await timeoutPromise;
console.log(promiseStateSync(timeoutPromise));
//=> 'fulfilled'
Asynchronously inspect the state of a promise.
Returns a promise for the state as a string with the possible values: 'pending'
, 'fulfilled'
, 'rejected'
.
Note: While this is async, it does return the state in the next microtask, which is almost right away.
Synchronously inspect the state of a promise.
Returns the state as a string with the possible values: 'pending'
, 'fulfilled'
, 'rejected'
.
Note: This method does not work in the browser.