Socket
Book a DemoInstallSign in
Socket

@nkp/is-promise

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nkp/is-promise

Utility functions for determining if values are Promises or Promise-Like's.

latest
Source
npmnpm
Version
2.0.1
Version published
Maintainers
1
Created
Source

@nkp/is-promise

npm version deploy status known vulnerabilities

Utility functions for determining if values conform to the Promise or PromiseLike interfaces.

// interface

/**
 * Does the unknown value conform to the Promise interface?
 *
 * @param value   any value
 * @returns       whether the value is Promise
 */
declare function isPromise<T>(value: PromiseLike<T> | unknown): value is Promise<T>;

/**
 * Does the unknown value conform to the PromiseLike interface?
 *
 * @param value   any value
 * @returns       whether the value is a PromiseLike
 */
declare function isPromiseLike<T>(value: PromiseLike<T> | unknown): value is PromiseLike<T>;

/**
 * Does the PromiseLike value conform to the Promise interface?
 *
 * @param like    PromiseLike value
 * @returns       whether the value is a Promise
 */
declare function andIsPromise<T>(like: PromiseLike<T>): like is Promise<T>;
// usage

import { isPromise, isPromiseLike, andIsPromise, } from '@nkp/is-promise';

// isPromiseLike
console.log(isPromiseLike(new Promise<void>((res) => res())));      // true
console.log(isPromiseLike(Promise.reject()));                       // true
console.log(isPromiseLike(Promise.resolve().catch(() => {})));      // true
console.log(isPromiseLike({ then() {}, catch() {}, }));             // true
console.log(isPromiseLike({ then: () => {}, catch: () => {}, }));   // true
console.log(isPromiseLike({ then: () => {}, }));                    // true
console.log(isPromiseLike({ catch: () => {}, }));                   // false

// isPromise
console.log(isPromise(new Promise<void>((res) => res())));      // true
console.log(isPromise(Promise.resolve()));                      // true
console.log(isPromise(Promise.reject().catch(() => {})));       // true
console.log(isPromise({ then() {}, catch() {}, }));             // true
console.log(isPromise({ then: () => {}, catch: () => {}, }));   // true
console.log(isPromise({ then: () => {}, }));                    // false
console.log(isPromise({ catch: () => {}, }));                   // false

// andIsPromise
// takes a PromiseLike value and determines if it is actually a promise

Table of contents

Installation

npm

npm install @nkp/is-promise

yarn

yarn add @nkp/is-promise

pnpm

pnpm add @nkp/is-promise

Publishing

To a release a new version:

  • Update the version number in package.json
  • Push the new version to the master branch on GitHub
  • Create a new release on GitHub for the latest version

This will trigger a GitHub action that tests and publishes the npm package.

Keywords

TypeScript

FAQs

Package last updated on 08 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts