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

@trenskow/custom-promise

v0.11.0

Published

A extendable class for making custom promises.

Downloads

17

Readme

custom-promise

A small JavaScript library for creating custom promises.

Reason

The promise specifications in JavaScript makes it hard to subclass Promise as the specifications does not allow for constructors that takes anything else but a function as a callback.

See the ECMA specifications for promises here

Solution

I've created this small class that follows the specifications for what is a promise. All promises needs to have a then method that takes two parameters – catch is syntactic sugar added by the JavaScript engine.

How to Use

It's dead simple!

You subclass CustomPromise and then you call either this._resolve(value) or this._reject(error) when the promise is done.

State and callback handling is handled under the hood.

The constructor for CustomPromise takes no parameters, so you can customize your subclasses to whatever needs, you might have. You do, though, need to call super() doing construction.

Example

import CustomPromise from '@trenskow/custom-promise';
import readline from 'readline';

class AskQuestion extends CustomPromise {

	constructor(question) {

		super()

		this._rl = readline.createInterface({
			input: process.stdin,
			terminal: true
		});

		this._rl.question(question, (answer) => {
			if (answer === '!') this._reject(new Error('User refused to answer!'))
			else this._resolve(answer);
		});

	}

}

Now you can use it like this.

await new AskQuestion('What is your name?');

Another example

The above use case for this might be a little impractical, as you might as well just implement it using regular promises or an asynchronous function.

But a useful use case is when you want to provide promise capability on daisy chained APIs.

import CustomPromise from '@trenskow/custom-promise';

class DoIt extends CustomPromise {

	constructor () {

		super()

		setImmediate(() => {
			this._exec();
		});

	}

	take() { return this; }

	your() { return this; }

	mama() { return this; }

	out() { return this; }

	all() { return this; }

	night() { return this; }
	
	_exec() {
		this._resolve('Baby, you\'re a full grown man!');
	}

}

This can be used like this.


await new DoIt()
	.take()
	.your()
	.mama()
	.out()
	.all()
	.night(); // Returns 'Baby, you\'re a full grown man!'

This is not really possible with promises as they are implemented in ECMAScript, and therefore you cannot really create APIs like this – but with CustomPromise you can..! 😉

License

See LICENSE.