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

retry-task

v1.0.1

Published

Retry a Task until it succeeds

Downloads

195

Readme

retry-task

Retry a Task a given number of times until it succeeds.

The module is built for use with Folktale's Task monad (data.task).

Build Status

Usage

The module exports an object with two functions.

retry(retries, fn)

The function accepts a number of retries indicating the maximum number of retries and fn, a function that returns a task. Note that the number of retries excludes the initial trial before failure. I.e. if fn always fails and retries = 2 then fn will be called three times in total.

delayedRetry(retries, delay, fn)

The function accepts a number of retries indicating the maximum number of retries (see above). The delay parameter can be either a number, indicating the time to wait between retries in milliseconds, or a function that returns such a number. A delay function is called before each retry with the next attempt number (1, 2, 3, ...) as the first parameter. The fn parameter is a function that returns a task.

Miscellaneous

Both functions are curried.

Examples

retry

const fetch = require('isomorphic-fetch')
const Task = require('data.task')
const { retry } = require('retry-task')

const fork = t =>
  t.fork(console.error, console.log)

// Basic
let calls = 0
const failTwice = () =>
  new Task((rej, res) =>
    calls++ >= 2 ? res('All good!') : rej('nope'))

const basic1 = retry(2, failTwice)
fork(basic1)

const alwaysFail = () =>
  new Task(rej => rej('ALWAYS BLUE!'))
const basic2 = retry(2, alwaysFail)

fork(basic2)

// Fetching URLs with retries
const fetchUrl = url =>
  new Task((rej, res) =>
    fetch(url)
      .then(r => res(`[${r.status}] ${r.statusText}`))
      .catch(rej))

const npm = retry(3, () => fetchUrl('http://www.npmjs.com/'))
fork(npm)

// since the function is curried we can also do this
const withRetries = retry(3)
const npm2 = withRetries(() => fetchUrl('http://www.npmjs.com/'))
const google = withRetries(() => fetchUrl('http://www.google.com/'))
fork(npm2)
fork(google)

delayedRetry

const fetch = require('isomorphic-fetch')
const Task = require('data.task')
const { delayedRetry } = require('retry-task')

const fork = t =>
  t.fork(console.error, console.log)

// Basic
let calls = 0
const failTwice = () =>
  new Task((rej, res) =>
    calls++ >= 2 ? res('All good!') : rej('nope'))

const basic1 = delayedRetry(2, 1000, failTwice)
fork(basic1)

const alwaysFail = () =>
  new Task(rej => rej('ALWAYS BLUE!'))
const basic2 = delayedRetry(2, 1000, alwaysFail)

fork(basic2)

// Fetching URLs with retries
const fetchUrl = url =>
  new Task((rej, res) =>
    fetch(url)
      .then(r => res(`[${r.status}] ${r.statusText}`))
      .catch(rej))

const npm = delayedRetry(3, 1000, () => fetchUrl('http://www.npmjs.com/'))
fork(npm)

// since the function is curried we can also do this
const withRetries = delayedRetry(3)
const withDelayedRetries = withRetries(1000)
const npm2 = withDelayedRetries(() => fetchUrl('http://www.npmjs.com/'))
const google = withDelayedRetries(() => fetchUrl('http://www.google.com/'))
fork(npm2)
fork(google)

// Custom delay function
const delay = attemptNo =>
  attemptNo * 1000 // attemptNo is always >= 1

const withCustomDelayedRetries = withRetries(delay)
const npm3 = withCustomDelayedRetries(() => fetchUrl('http://www.npmjs.com'))
fork(npm3)