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-flow-extensions

v2.0.0

Published

Extends the Promise object with some additional utility functions

Downloads

5

Readme

Promise Flow Extensions

This are simple extensions to make handling some common promise flows a bit more simple.

Instalation:

$ npm install --save promise-flow-extensions

Usage

The library can be used in three ways

Directly accessing the helper methods:

const promiseFlowExt = require('promise-flow-extensions');

promiseFlowExt.if(Promise.resolve(2), {condition: v => v > 1, true: v => v - 1})
  .then(console.log); // 1

Important! when using functions this way, the first parameter is always the promise

Extending a specific promise:

const promiseFlowExt = require('promise-flow-extensions');

const eventually2 = Promise.resolve(2);
promiseFlowExt.extend(eventually2);
eventually2.if({condition: v => v > 1, true: v => v - 1})
  .then(console.log); // 1

Extending all promises

const promiseFlowExt = require('promise-flow-extensions');
promiseFlowExt.extend(Promise.prototype);

const eventually2 = Promise.resolve(2);
eventually2.if({condition: v => v > 1, true: v => v - 1})
  .then(console.log); // 1

Removing the extensions

You can reset an extended object (i.e. remove all the added methods) by running

const promiseFlowExt = require('promise-flow-extensions');
promiseFlowExt.extend(Promise.prototype);
promiseFlowExt.reset(Promise.prototype);

const promise = Promise.resolve(1);
promise.rethrow(() => null) // promise.rethrow is not a function
  .then(console.log);

Methods

All methods can be called using the 3 options shown above, we'll assume we have extended all promises for all examples.

rethrow(function)

Catches and exception and executes the passed function. Then it rethrows the exception received. The function receives the thrown error as an argument.

Promise.reject(new Error('test'))
  .rethrow(e => console.log(e.message)) // test
  .then(v => 'this is never called')
  .catch(e => /* actually handle the error */);

if(trueFn | {condition, true, false})

Executes the passed function if the promise is true. Instead of a function you can provide an object, specify up to 3 values, if any of those values isn't specified, the default is the identity function, i.e. v => v

  • condition: a function that will be used to evaluate if the promise result is true/false
  • true: The function that will be executed when the condition is true
  • false: The function that will be executed when the condition is false
Promise.resolve('test')
  .if({
    condition: v => v === 'test',
    true: v => v + ' ok!',
    false: v => 'this is never called'
  }) // 'test ok!'
  .then(console.log); // 'test ok!'

retry(function, [condition, [options]])

Executes the passed function. It will check the result (or the result of calling condition with the result if a condition is provided). If it's false it will retry the function options.retries times (default: 1), waiting options.interval milliseconds (default 1000) between retries.

option.interval is a function, that is called each time with the number of retries remaining.

If retries run out it will reject the promise with an exception.

Promise.resolve('http://example.org/my-simple-service')
  .retry(u => callService(u), r => r.body === 'ok', {
    retries: 10,
    interval: retries => (10 - retries) * 1000
  })
  .catch(e => {body: 'failed'})
  .then(console.log); // something like {body: ok} ok {body: 'failed'}