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

progress-promise

v0.0.6

Published

Promise subclass with mechanism to report progress before resolving

Downloads

264

Readme

progress-promise Build Status npm version

Promise subclass with mechanism to report progress before resolving

class ProgressPromise extends Promise

constructor(executor)

  • executor <Function> Invoked immediately
    • resolve <Function> Same as original Promise
    • reject <Function> Same as original Promise
    • progress <Function> Before resolving, pass single argument to progress listener (May be invoked multiple times)

Executor function receives extra argument: progress, a function to be called to notify a listener before resolving.

const ProgressPromise = require('progress-promise');

function longTask() {
  return new ProgressPromise((resolve, reject, progress) => {
    setTimeout(() => progress(25), 250);
    setTimeout(() => progress(50), 500);
    setTimeout(() => progress(75), 750);
    setTimeout(resolve, 1000);
  });
}

progress(handler)

  • handler <Function> Invoked by progress function passed to executor
    • value <Function> Value from executor

Promise rejects if progress handler throws.

longTask()
  .progress(value => console.log(value + '%'))
  .then(() => console.log('Done'));

// 25%
// 50%
// 75%
// Done

static all(promises)

  • promises <Array(Promise)> Array of Promise, or compatible

Like Promise.all() but the results are passed to the progress listener as an Array after each completion.

A custom property, proportion is added to this results array containing the value of the number of Promises resolved divided by the total number of Promises.

function delay(duration) {
  return new Promise(resolve =>
    setTimeout(() => resolve(duration), duration));
}

ProgressPromise.all([ delay(300), delay(100) ])
  .progress(results => console.log('Progress', results))
  .then(results => console.log('Resolved', results));

// Progress [ , 100, proportion: 0.5 ]
// Progress [ 300, 100, proportion: 1 ]
// Resolved [ 300, 100, proportion: 1 ]

static sequence(inputs, handler)

  • inputs <Array> Input values to be passed to handler sequentially
  • handler <Function> Invoked for each input value, must return Promise
    • value From inputs array

Handler is invoked once for each input value, starting with the first index and proceding after each Promise returned resolves.

Progress is reported the same as ProgressPromise.all().

ProgressPromise.sequence([ 200, 100 ], value => delay(value))
  .progress(results => console.log('Progress', results))
  .then(results => console.log('Resolved', results));

// Progress [ 200, proportion: 0.5 ]
// Progress [ 200, 100, proportion: 1 ]
// Resolved [ 200, 100, proportion: 1 ]

Design Considerations

Single argument for progress listeners

The function that invokes each listener on progress updates only has a single argument available due to the lack of support for spread operators in the target Node.js version this package aims to support, 4.3.

value => this[LISTENERS].forEach(listener => listener(value))

If a normal (not arrow) function is used here, this cannot be accessed before super() is called, making it impossible to bind to the listeners instance property.

Symbols as private properties

The new ES6 Symbol() type creates a non-enumerating value that can be used as a key on an object. Creating a key on an object instance with a Symbol can be similar to creating a private property if the Symbol is not shared. If Symbols are not available, a fallback string property key is used.

License

MIT