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

co-retry-it

v0.1.1

Published

Execute function which returns yieldable object with retry mechanism when error happens.

Downloads

5

Readme

co-retry-it

npm package

Execute function which returns a yieldable object with retry mechanism when error happens.

When using co/koa, if your asynchronous function sometimes fail, wrap it with co-retry-it, which will retry it in the way you customized.

install

npm i co-retry-it

example

var retry = require('co-retry-it')
var co = require('co')

function* failGenerator () {
    throw new Error(new Error('something wrong'))
}

co(function* () {
    var result = yield retry(failGenerator, {times:5})
    console.log(result)
})

function generatePromise (num) {
    return new Promise(function (resolve, reject) {
        setTimeout(function(){
            var result = Math.random()
            if (result > num){
                reject(new Error('result is too big'))
            } else {
                resolve(result)
            }
        }, 100)
    })
}

co(function* () {
    var result = yield retry(generatePromise.bind(null, 0.2))
    console.log(result)
})


parameter

retry(func, options) func, function that returns a yieldable object. options, an object including extra options.

options

times Number or Function

the number of times the function will retry at most when error happens to yiedable object or a function that receives an argument 'error' and returns boolean showing whether need retry

default value is 3

example:

function getData () {
    return new Promise(function (resolve, reject) {
        var data = Math.random()
        if (data < 0.45) {
            reject(new Error('data is too small'))
        } else if (data > 0.55) {
            reject(new Error('data is too big'))
        } else {
            resolve(data)
        }
    })
}

co(function* () {
    var result = yield retry(getData, {
        times: function (err) {
            // retry only when data is too small
            return err.message === 'data is too small'
        }
    })
    console.log(result)
})

arguments Function

a function generates an arguments array for execution and that receives an argument 'error' which is the last error(would be undefined in first execution)

remember to return an array

 //simulate a crowded network
 //and need to seed data with an exponential backoff algorithm

function sendData (waitTime) {
    console.log('try to send data')
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            if (waitTime < 512) {
                console.log('after ' + waitTime* 10 + ' milliseconds, failed')
                reject(new Error('network error'))
            }  else {
                console.log('after ' + waitTime* 10 + ' milliseconds, success')
                resolve('success')
            }
        }, waitTime* 10)

    })
}

co(function* () {
    var waitTime
    var result = yield retry(sendData, {
        times: 30,
        arguments: function(err) {
            // err would be undefined when first execution
            // if the err is not network error we can reset the waitTime
            if (err === undefined || err.message !== 'network error') {
                waitTime = 1
            }
            var wt = waitTime
            waitTime *= 2
            // do not forget the return value should be an array!
            return [wt]
        }
    })
    console.log(result)
})