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

node-endeavour

v1.0.0

Published

Flexible, fault-tolerant operations for Typescript.

Downloads

3

Readme

Endeavour

styled with prettier Coverage Status CircleCI

Flexible, fault-tolerant operations for Typescript.

Requirements

  • Node >=4.6.0
  • Typescript >=2.0
  • Experimental decorators
  • ES6 target (for async/await support)

Installation

npm install node-endeavour --save

Example Usage

import endeavour from 'node-endeavour'

function unreliable () {
  if (Math.random <= 0.25) return Promise.resolve()
  return Promise.reject()
}

// create a fault tolerant operation. This will be retried
// 10 times, backing off exponentially with no maximum delay.
const faultTolerant = endeavour(unreliable)

async function main () {
  let result

  try {
    result = await faultTolerant()
  } catch (error) {
    console.log(error)
  }
}

main()

API

endeavour(thenableFunction, [options], [ctx])

Creates a new EndeavourRunnable; a wrapped version of your function that will retry indefinitely, or up until some specified limit. The default options are as follows:

  • retries The maximum amount of times to retry the operation. Defaults to 10.
  • constant The base in exponential backoff, or the slope in linear backoff. Defaults to 2.
  • minTimeout The number of milliseconds before starting the first retry. Default is 1000.
  • maxTimeout The maximum number of milliseconds between two retries. Default is Infinity.

You may also specify this (useful for wrapping instance methods).

class Thing {
  doSomethingUnreliable () {}
}

const instance = new Thing()

const faultTolerant = endeavour(instance.doSomethingUnreliable, instance)

faultTolerant()
  .then(res => { console.log('yay!') })
  .catch(e => { console.log(':(')) })

See the decorators section for a more terse way of handling these cases.

EndeavourRunnable([arguments], [intercept])

Using your wrapped function from endeavour(...) works much in the same way as before. Arguments are be passed like this:

myFaultTolerantOperation(['some', 'arguments'])

The wrapped operation also accepts and optional function to allow for greater control over how retries should proceed.

faultTolerantOperation((result, next) => {
  if (result.error instanceof MyCustomError) {
    return next(new Error('something went horribly wrong :('))
  }
})

Calling next() with an error will stop your operation. You can also pass in an new array of arguments for subsequent retries.

faultTolerantOperation((result, next) => {
  if (result.error instanceof MyCustomError) {
    return next(['new', 'args'])
  }
})

You could also just inspect the result of the last attempt.

faultTolerantOperation(result => {
  console.log(result.error)
})

Decorators

@endeavourable(EndeavourOpts)

A decorator for providing metadata about how retriable methods in a class should operate. Takes the options defined here.

@endeavourify([EndeavourOpts])

Provides the same functionality as endeavour, but for class methods.

@endeavourable({ retries: 15 })
class {
  // retried 15 times
  @endeavourify()
  unreliableMethod () {
    if (Math.random <= 0.25) return Promise.resolve()
    return Promise.reject()
  }

  // retried 5 times
  @endeavourify({ retries: 5 })
  doSomething () { }

  // retried 10 times
  @endeavourify()
  otherThing() { }
}

When no arguments are given to @endeavourable or @endeavourify, the defaults provided here will be used.

Want something for plain old JS? Check out node-retry.