npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-delegate

v1.0.1

Published

Object-Oriented Promise Wrapper

Downloads

1,001

Readme

npm version Build Status Coverage Status

PromiseDelegate

A simple type-safe wrapper around a Promise that provides an object-oriented interface for settling (resolving/rejecting) the promise.

Written in TypeScript, and distributed as both an ES module and CJS module with type definitions and source maps.

Contents

Why? (example)

Sometimes code is designed/organized in such a way that implementing a Promise becomes very tedious. For example, if you need to instantiate and have a reference to your Promise before and separate from code that triggers the eventual settling of that Promise.

NOTE: I'm not promoting this as "good practice" in general if avoidable, but just acknowledging that it is sometimes practically/pragmatically unavoidable for a variety of reasons.

Here's an oversimplified example with a raw Promise:

class Foo {
    private readonly namePromise: Promise<string>;
    private resolveNamePromise!: (value: string) => void;

    constructor() {
        this.namePromise = new Promise<string>((resolve) => {
            // Store the resolve function so we can call it
            // later.
            this.resolveNamePromise = resolve;
        });
    }

    public getName(): Promise<string> {
        return this.namePromise;
    }

    public initStuff(): void {
        // hardcoded value for simplified example only
        this.resolveNamePromise("Bob");
    }
}

Here's the same example, but simplified by using PromiseDelegate:

import { PromiseDelegate } from "promise-delegate";

class Foo {
    private readonly namePromiseDelegate = new PromiseDelegate<string>();

    public getName(): Promise<string> {
        return this.namePromiseDelegate.promise;
    }

    public initStuff(): void {
        // hardcoded value for simplified example only
        this.namePromiseDelegate.resolve("Bob");
    }
}

Installation

Install via NPM:

npm i -s promise-delegate

Usage

Import

TypeScript/ES6:

import { PromiseDelegate } from "promise-delegate";

JavaScript:

const PromiseDelegate = require("promise-delegate").PromiseDelegate;

Instantiate

constructor<ValueType = void>(ignoreMultipleSettles: boolean = false)

Use the PromiseDelegate constructor to create a new PromiseDelegate with a corresponding new underlying Promise.

The ValueType type parameter specifies the type of resolve value. Defaults to void if unspecified, which represents a PromiseDelegate that can resolved without a value to simply indicate that something has finished.

The optional ignoreMultipleSettles parameter controls what happens if you attempt to settle (resolve/reject) the PromiseDelegate after it has already been settled:

  • false: (default) An exception is thrown upon subsequent attempts to settle.
  • true: Subsequent attempts to settle are silently ignored.

TypeScript:

// specify type of the promise value
const numberPromiseDelegate = new PromiseDelegate<number>();

// defaults to a `void` promise if type is unspecified
// (no resolved value; only signals the completion of something)
const voidPromiseDelegate = new PromiseDelegate();

JavaScript:

// You're on your own for the type of the value :)
const promiseDelegate = new PromiseDelegate();

Properties

promise

promise: Promise<ValueType>

A reference to the underlying Promise that can be settled (resolved/rejected) by the PromiseDelegate.

settled

settled: boolean;

True if this PromiseDelegate has been settled (resolved/rejected).

Methods

resolve

resolve(value: ValueType | PromiseLike<ValueType>): void

Resolves the underlying Promise with the specified value and marks this PromiseDelegate as settled.

Throws an error if this PromiseDelegate was already previously settled, and it was not instantiated with allowMultipleSettles = true.

NOTE: If ValueType is void, then the value parameter may be omitted completely, or must be exactly undefined if specified.

reject

reject(reason?: any): void

Rejects the underlying Promise with the specified reason and marks this PromiseDelegate as settled.

Throws an error if this PromiseDelegate was already previously settled, and it was not instantiated with allowMultipleSettles = true.