p-state

Inspect the state of a promise


Keywords
promise, state, inspect, inspection, reflect, reflection, introspect, introspection, pending, fulfilled, rejected, npm-package
License
MIT
Install
npm install p-state@0.1.1

Documentation

p-state

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.

Install

$ npm install p-state

Usage

const timers = require('timers/promises');
const {promiseStateSync} = require('p-state');

(async () => {
	const timeoutPromise = timers.setTimeout(100);

	console.log(promiseStateSync(timeoutPromise));
	//=> 'pending'

	await timeoutPromise;

	console.log(promiseStateSync(timeoutPromise));
	//=> 'fulfilled'
})();

API

promiseStateAsync(promise: Promise)

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.

promiseStateSync(promise: Promise)

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.

Related

  • p-reflect - Make a promise always fulfill with its actual fulfillment value or rejection reason
  • p-settle - Settle promises concurrently and get their fulfillment value or rejection reason
  • More…