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

@shivams136/advanced-promise

v2.1.1

Published

A simple wrapper class for Promise class, which add multiple advanced functionality to the default promise like Aborting, data binding, status checking.

Downloads

10

Readme

advanced-promise

A simple extension to the native Promise API

Overview

This simple library provides you some handy extensions to the native Promise API like below:

  • Make the promise/fetch abortable
  • Get status of the promise at any stage
  • Bind some data to the promise and get that
  • Resolve/Reject the promise from outside
  • Auto Timeout for aborting the promise after particular time in microseconds

How to install

Simply run below:

npm install @shivams136/advanced-promise

How to use

Signature

AdvancedPromise(([resolve] [, reject] [, signal])=>{
    // ...
} [,timeoutInMs][,data]): AdvancedPromise

Creating

There are below 2 ways to create an AdvancedPromise:

1. Constructor

const advPromise = new AdvancedPromise(
	(resolve, reject, signal) => {
		//....
	},
	timeout,
	data
);

2. Existing Promise

// Pass existing promise to the static `from` method
const advPromise = AdvancedPromise.from(promise);

Abort

// Abort with default reason - Aborted
advPromise.abort();

// Abort with custom reason
advPromise.abort("I just want to abort");

// cancel is alias of abort so you can use that too
advPromise.cancel();

Getters

Abort Reason

This will provide you the abort reason for the promise. Its value will be undefined if the proise is not aborted yet, so you can use is to check if the promise is aborted or not.

advPromise.abortReason; // default: Aborted

Status Methods

isFulfilled

Check whether the promise is fulfilled or not

advPromise.isFulfilled; // true/false

isSettled

Check whether the promise is settled/resolved or not

advPromise.isSettled; // true/false

isRejected

Check whether the promise is rejected or not

advPromise.isRejected; // true/false

isTimedout

Check whether the promise is aborted due to timeout or not

advPromise.isTimedout; // true/false

status

This will provide you the current status of the promise

advPromise.status; // "pending" | "resolved" | "rejected"

More features

Resolve/Reject from outside

You can easily resolve/reject the promise from outside by just calling resolve() or reject() methods from outside.

advPromise.resolve("Test Data");
advPromise.reject("Test Error");

Examples:

Use with Fetch and a timeout value

const loadData = (id) => {
	return new AdvancedPromise((resolve, reject, abortSignal) => {
		fetch(url, { signal: abortSignal })
			.then((response) => response.json())
			.then(parsedData)
			.then(resolve)
			.catch(reject);
	}, 5000); // 5000ms timeout
};

const advPromise = loadData(id);
advPromise.abort(); // For manual abort

Hook the abort

You can add event listener on Abort action and do anything on abort of the error

const advPromise = new AdvancedPromise((resolve, reject, abortSignal) => {
	abortSignal.addEventListener("abort", () => {
		// Do something when abort happens
	});
	// ...
});

Use status

const advPromise = new AdvancedPromise((resolve, reject, abortSignal) => {
	// ...
});
//...
//...
//...
const iconColor = advPromise.isFulfilled ? (advPromise.isSettled ? "green" : "red") : "orange";

Use Data

const advPromise = new AdvancedPromise(
	(resolve, reject, abortSignal) => {
		// ...
	},
	5000, // 5 second timeout
	{ name: "Shivam", age: 25 } // Data bound with promise
);
//...
//...
//...
const promiseData = advPromise.data; // {name:"Shivam",age:25}

Working Example with plain JS

You can find the code sandbox here.

import AdvancedPromise from "@shivams136/advanced-promise";

const loadData = (id) => {
	return new AdvancedPromise((resolve, reject, abortSignal) => {
		fetch(`https://jsonplaceholder.typicode.com/posts/${id}`, {
			signal: abortSignal,
		})
			.then((response) => response.json())
			.then(resolve)
			.catch(reject);
	});
};

const id = 1;
const advPromise = loadData(id);
advPromise
	.then((myData) => {
		console.log(myData);
	})
	.catch((err) => console.log("My Error:\n", err));
advPromise.abort();

A working example on node

You can find the code sandbox here.

const fetch = require("node-fetch");
const AdvancedPromise = require("@shivams136/advanced-promise");

const loadData = (id) => {
	return new AdvancedPromise((resolve, reject, abortSignal) => {
		fetch(`https://jsonplaceholder.typicode.com/posts/${id}`, { signal: abortSignal })
			.then((response) => response.json())
			.then(resolve)
			.catch(reject);
	});
};

const id = 1;
const advPromise = loadData(id);
advPromise
	.then((myData) => {
		console.log(myData);
	})
	.catch((err) => console.log("My Error:\n", err));
advPromise.abort();

Contact

You can contact me on github anytime.

This package is inspired by this package from Thor(Shenghan) Chen.