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

@ashwynh21/completer

v1.1.3

Published

A simple utility class based on the dart completer that allows de-contextualizing promises

Downloads

6

Readme

Completer

A simple class that allows you to de-contextualize promises just like in dart

How to install

npm install @ashwynh21/completer

It also has TS support.

How to use

The Completer class can be used on both Browser and NodeJS since it is based on Promise instances. All it does is wrap a promise instance along with a few other things that allow you to access the control properties of the promise instance outside. i.e. complete and reject.

Here is a set of use cases:

Browser

Let's say you want to run an HTTP request to some API that will trigger a loader and provide some data so that you can add that data to an event listener on some button.

Now, when the user clicks on this button you want to resolve some initially running feature (loader). so that the loader only stops when the response comes back and, the user clicks on the button.

// import the completer class
import { Completer } from '@ashwynh21/completer';

// in comes the completer
const button = document.querySelector('button');
// we define a completer instance
const completer = new Completer();
// then get the loader
const loader = document.querySelector('#loader');

const api_call = async () => {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, Mat.floor(Math.random() * 1E4));
    })
};

// before calling the api request we start the loader
loader.start();
// then we tell the completer promise what to do when complete
completer.then(() => loader.stop());

api_call().then(() => button.addEventListener('click', () => {
    // then we resolve the completer, triggering the then callback to stop the loader
    completer.complete()
}));

NodeJS

let's say you have a series of API hooks, for instance, a payment set. Say a user triggers a payment flow on their UI and you API triggers a request to your payment provider. Your provider will then hook the results of the payment back on another function that you have defined.

So let us say you wanted to complete the users request, when the provider hooks back in on the second function you would have defined.

// import the completer class
import { Completer } from '@ashwynh21/completer';

const transactions: Record<string, Completer> = {};

// your initial payment hook
const payment = (id: string) => {
    // make payment request to provider
    transactions[id] = new Completer();
  
    // so by returning the completers promise, the response will only resolve once the completer is completed
    return transactions[id].promise;
}

// the provider hook
const result = (data: { id: string }) => {
    // so then here we complete the users requests since the payment has been confirmed
    return transactions[data.id].complete(data);
}

I hope this tiny class does the same wonders that it has done for me.

Class Properties and Methods

Constructor

The class constructor takes an options object that allows you to set custom features, i.e.:

| Name | Description | | ----------- | ---------------------------------------------------------------------------------------- | | timeout | Allows you to set a timeout limit on the completer | | error | Allows you to apply a custom error to be thrown if the completer comes across an error |

Methods

| Name | Description | | ----------------------------- | ---------------------------------------------- | | complete | Called when resolving the completer promise. | | reject | Called when rejecting the completer promise. | | then, catch & finally | Extended from thePromise class |

Contributions

This is an open source project, and its pretty simple. Please feel free to make PRs considering any ideas that would make this class more sensible and well fleshed out.