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-with-order

v0.1.0

Published

Micro module to resolve multiple promises in order.

Downloads

1

Readme

promise-with-order

Micro module to resolve multiple promises in order.

Lot of time you want to trigger multiple async task together but you want them to get resolved sequentially. PromiseWithOrder wraps passed promise to a promise which resolves in sequential order.

Installation

npm install promise-with-order

Example

import PromiseWithOrder from 'promise-with-order';

/**
 * Create a instance of PromiseWithOrder in which all wrapped promise will be resolved sequentially.
*/
const pwo = new PromiseWithOrder();

/* 
  Usage
*/

const promise1 = pwo.wrap(asyncTask()); //asyncTask task return a promise here which resolves after 1s.
const promise2 = pwo.wrap(somePromise); //promise which resolves after .5s
const promise3 = pwo.wrap(Promise.resolve('Hello World!!'));

promise1.then(() => {
  console.log('Resolved Promise1.');
});

promise2.then(() => {
  console.log('Resolved Promise2.');
});

promise3.then((data) => {
  console.log('Resolved Promise3.');
  console.log(data);
});

/** log will be 
  Resolved Promise1.
  Resolved Promise2.
  Resolved Promise3.
  Hello world
**/

Notice here even the promise resolves in any order, the wrapped promise are always resolved in order.

Promise rejections

By default if any of the promise fails, all the subsequent wrapped promise will fail. This is considering you need resolve to be in order only when a resolution of promise is dependent on previous resolution.

const promise1 = pwo.wrap(asyncTask()); //asyncTask task return a promise here which resolves after 1s.
const promise2 = pwo.wrap(Promise.rejects('error in promise 2')); 
const promise3 = pwo.wrap(somePromise); //promise which resolves after .5s

promise1.then(() => {
  console.log('Resolved Promise1.');
});

promise2.catch((err) => {
  console.log(`Promise 2 got rejected because of ${err}.`);
});

promise3.then(() => {
  console.log('Resolved Promise3.');
}, (err) => {
  console.log(`Promise 3 got rejected because of ${err}.`);
});

/** log will be 
  Resolved Promise1.
  Promise 2 got rejected because of error in promise 2.
  Promise 3 got rejected because of error in promise 2.
**/

But in case a promise does not have side effect on next promise but still has to be resolved in order. You can pass allowReject flag.

const promise1 = pwo.wrap(asyncTask()); //asyncTask task return a promise here which resolves after 1s.
const promise2 = pwo.wrap(Promise.rejects('error in promise 2'), {allowReject: true}); 
const promise3 = pwo.wrap(somePromise); //promise which resolves after .5s

promise1.then(() => {
  console.log('Resolved Promise1.');
});

promise2.catch((err) => {
  console.log(`Promise 2 got rejected because of ${err}.`);
});

promise3.then(() => {
  console.log('Resolved Promise3.');
}, (err) => {
  console.log(`Promise 3 got rejected because of ${err}.`);
});

/** log will be 
  Resolved Promise1.
  Promise 2 got rejected because of error in promise 2.
  Resolved Promise3..
**/

Resetting PromiseWithOrder instance

You can use .reset method to reset PromiseWithOrder instance.

const promise1 = pwo.wrap(p1);
const promise2 = pwo.wrap(p2);

pwo.reset();

const promise3 = pwo.wrap(p3);
const promise4 = pwo.wrap(p4);

In this promise1 and promise2 will be resolved in order. Same for the promise3 and promise4. But promise3 and promise 4 is independent of promise1 or promise2.