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

@kanthoney/extended-promise

v0.1.4

Published

promise with extra utilities

Downloads

4

Readme

extended-promise

Native promise extended with extra utilities like map and reduce

Installation

npm install --save @kanthoney/extended-promise

Usage

const Promise = require('@kanthoney/extended-promise');

Methods

  • tap(f)

Calls f on the resolved value, returning the original value

Promise.resolve(5)
.tap(result => {
  return result + 1;
}).then(result => {
  // result is 5 not 6
})
  • tapCatch(f)

Like tap but for catch.

Promise.reject('error').tapCatch(error => {
  return 'success';
}).catch(error => {
  // error is 'error'
})
  • delay(t)

Introduces a delay in milliseconds

Promise.resolve(result)
.delay(500)
.then(result => {
  // called after 500 ms
});
  • disposer(f)

Used with using for disposing resources. f is a function which takes a resource and disposes of that resource.

  • all

Calls Promise.all on the resolved value. Assumes the resolved value is an array.

  • each(f, options)

Calls Promise.each on the resolved value. Assumes the resolved value is an array.

  • map(f, options)

Calls Promise.map on the resolved value. Assumes the resolved value is an array.

  • mapSeries(f)

Calls Promise.mapSeries on the resolved value. Assumes the resolved value is an array.

  • reduce(f)

Calls Promise.reduce on the resolved value. Assumes the resolved value is an array.

  • props()

Calls Promise.props on the resolved value. Assumes the resolved value is an object.

static methods

  • each(a, f, options)

calls function f, which may return a promise, on each item of array or iterator a. Returns a promise resolving to undefined or the value of the first rejected promise in the case of an error. options is an object for configuration parameters. There is currently one parameter, concurrency, which is the maximum number of promises called simultaneously.

  • map(a, f, options)

Calls function f, which may return a promise, on each of item of array or iterator a. Returns an array of the resolved values f(item), in the same order as a even if the values were resolved in a different order. options is a configuration object currently with one parameter concurrency which is the maximum number of promises called simultaneously.

  • mapSeries(a, f)

Like map but with a concurrency of 1.

  • reduce(a, f, acc)

For each item in array or iterator a, calls f with parameters (acc, item). The return value of f is passed to the next call as the acc parameter. Returns the final acc value. acc defaults to 0.

Promise.reduce([1, 2, 3], (acc, i) => Promise.resolve(acc + i), 0)
.then(result => {
  //result = 1 + 2 + 3 = 6
});
  • props(a)

a is an object where the object properties can be promises. Returns an object of the same format with the properties resolved.

Promise.props({
  db: getDB(), // can return a promise
  config: getConfig() // can return a promise
}).then(({ db, config }) => {
  // db and config are the resolved values of getDB() and getConfig()
});
  • filter(a, f)

Returns an array of those items of a for which f(item) resolves to a truthy value.

Promise.filter([1,2,3,4,5], i => Promise.resolve(i%2 === 0))
.then(result => {
  // result = [2,4]
});
  • fromCallback(f)

Used to call a function that takes a callback and return a promise. f is a function the takes one parameter, a callback function. In the example below, db.query is a function that takes a callback in the old node style and executes a database query q with parameter values v.

Promise.fromCallback(done => {
  db.query(q, v, done);
}).then(result => {
  // result contains the database query result
});
  • coroutine(f)

Used with generator functions which yield promises. f is a generator function.

Say we have a function nextPage() which calls a backend and returns a promise which resolves to the next page of results. We can use coroutine to concatenate up to n pages as follows:

const acc = Promise.coroutine(function*(n) {
  let a = [];
  for(let i = 0; i < n; i++) {
    let page = yield nextPage();
    if(!page) {
      break;
    }
    a = a.concat(page);
  }
  return a;
});

acc(5).then(pages => {
  // pages contains 5 pages
});

coroutine creates the function acc. When this is called, acc calls the generator function with its parameters. Each time the nextPage function yields a promise, coroutine passes the resolved value to the page variable.

  • method(f)

Converts a function f which may return a promise into one that definitely does.

const f = Promise.method((a, b) => a + b);
f(5, 6).then(result => {
  // result = 11
});
  • using(disposer, f)

Used to guarantee the disposal of a resource after it's been used. The first argument is an object of the Disposer class, obtained by calling the disposer method, which takes a function which will dispose of the resource after using is finished. f is a function which takes the resource and returns the required result.

Say getDB is a function which returns a database handle and we want to call a function with this handle, and close the handle afterwards whether or not the call was successful. We would use something like the following:

Promise.using(
  getDB().disposer(db => db.close()),
  db => doQuery(q, v)
).then(result => {
  // result contains the result of the database query
})