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

hodash.retrie

v0.0.5

Published

A promise retrier with the ability to track and cancel externally and internally.

Downloads

23

Readme

GitHub license npm version PRs Welcome

What is a retrie?

A retrie is a retrier like you'd find from bluebird-retry or async-retry, but returns a modified promise that lets you track or cancel the retry process from both within the retry and externally. A good example of when this would be useful is if you have a case where you are reaching out to multiple data sources for something in a race scenario, and some data sources involve retries, this allows you to cancel the rest of them upon any succeeding.

Installation

npm install --save retrie

Usage

First, import retrie:

import { retrie } from 'hodash.retrie';

You can then use retrie to create a retried operation:

const operation = () => fetch('https://api.example.com/data');
const retried = retrie(operation, { maxRetries: 3, minTimeout: 1000 });

This creates a new operation that will be retried up to 3 times, with a minimum delay of 1000 milliseconds between attempts.

The retried object has a promise property containing the promise for the resolution of its function.

const result = await retried.promise;

// or

retried.promise
.then((result) => {})
.catch(err => {});

Canceling

You can cancel a retry process from happening from within your function or from outside.

// From outside
const retried = retrie(operation, { maxRetries: 3, minTimeout: 1000 });
retried.cancel();

// From inside

retrie(({ cancel, retries }) => {
  // Maybe something happened where in this particular case, after n retries, we cancel early
  if (retries > 3 && thingHappened) cancel();
});

// Specific cancel error
retrie(({ cancel, retries }) => {
  // We can pass cancel an error that will be thrown in place of the last error the retrie encountered.
  if (retries > 3 && thingHappened) cancel(new Error('Special error));
});

Scenario

Race Scenario

Consider a scenario where you are reaching out to multiple data sources in a race scenario. If any of the sources resolve successfully, you might want to cancel the rest:

const source1 = retrie(fetchDataFromSource1, { maxRetries: 3, minTimeout: 1000 });
const source2 = retrie(fetchDataFromSource2, { maxRetries: 3, minTimeout: 1000 });

const result = await Promise.race([source1.promise, source2.promise])
.catch(err => {}); // Swallow any errors from race failures

// Cancel any active retries (the "winner" will be unaffected)
source1.cancel();
source2.cancel();

Contributing

We welcome contributions! Please see our contributing guidelines for more information.

License

MIT