Star 历史趋势
数据来源: GitHub API · 生成自 Stargazers.cn
README.md

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.

Vote up this issue if you want to see this feature being included in Node.js itself.

Install

npm install p-state

Usage

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'

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.

import timers from 'node:timers/promises'; import {promiseStateAsync} from 'p-state'; const timeoutPromise = timers.setTimeout(100); console.log(await promiseStateAsync(timeoutPromise)); //=> 'pending' await timeoutPromise; console.log(await promiseStateAsync(timeoutPromise)); //=> 'fulfilled'

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…

关于 About

Inspect the state of a promise
inspectintrospectionnpm-packagepromise

语言 Languages

JavaScript97.0%
TypeScript3.0%

提交活跃度 Commit Activity

代码提交热力图
过去 52 周的开发活跃度
3
Total Commits
峰值: 2次/周
Less
More

核心贡献者 Contributors