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.consecutively

v1.0.1

Published

When you need to chain many promises and the order of completion matters

Downloads

3

Readme

promise.consecutively

When you need to chain many promises and that the order of completion matters.

Scenario

Let's say you have an unknown list of promises that need to resolve in order. You want promise3 to be fired only after promise2 has successfully resolved, and promise2 to be fired only after promise1 has successfully resolved.

When these promises are well-known in advance you can chain them like this :

promise1.then(() => promise2).then(() => promise3);

In the scenario where these promises are a dynamic list and their total number cannot be known in advance you can use the consecutively.iterate function to iterate over a list.

consecutively.iterate([promise_1, promise_2, promise_3, ...., promise_n], callBackFn).then(()=>{})

Demo

See the demo on stackblitz

How to use in Javascript

In this example, we have three promises that takes some seconds to resolve. I passed also the console.info function as callback to display the resolved response of each promises.

var promiseConsecutively = require('promise.consecutively').consecutively;

promiseConsecutively
  .iterate(
    [
      new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('number one - ' + new Date());
        }, 1000);
      }),
      new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('number two - ' + new Date());
        }, 5000);
      }),
      new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('number three - ' + new Date());
        }, 10000);
      })
    ],
    console.info
  )
  .then((result) => console.log('results', result));

The output is the following :

'number one - Thu Dec 03 2020 15:02:54 GMT+0100 (Central European Standard Time)';

'number two - Thu Dec 03 2020 15:02:58 GMT+0100 (Central European Standard Time)';

'number three - Thu Dec 03 2020 15:03:03 GMT+0100 (Central European Standard Time)';

'results'[
  'number one - Thu Dec 03 2020 15:02:54 GMT+0100 (Central European Standard Time)',
  'number two - Thu Dec 03 2020 15:02:58 GMT+0100 (Central European Standard Time)',
  'number three - Thu Dec 03 2020 15:03:03 GMT+0100 (Central European Standard Time)'
];

How to use in Typescript

import { consecutively } from "promise.consecutively";

interface TestInterface {
  i_attr: string;
}

const p1: Promise<TestInterface> = new Promise<TestInterface>(
  (resolver, _reject) => {
    resolver({ i_attr: "number 1" });
  }
);
const p2: Promise<TestInterface> = new Promise<TestInterface>(
  (resolver, _reject) => {
    resolver({ i_attr: "number 2" });
  }
);
const p3: Promise<TestInterface> = new Promise<TestInterface>(
  (resolver, _reject) => {
    resolver({ i_attr: "number 3" });
  }
);

consecutively
  .iterate<TestInterface>([p1, p2, p3], console.log)
  .then((response) => {
    console.log(response);
  });