Socket
Socket
Sign inDemoInstall

ejmorgan-retry

Package Overview
Dependencies
2
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ejmorgan-retry

A simple and lightweight package to retry various execution contexts.


Version published
Weekly downloads
344
increased by57.8%
Maintainers
1
Install size
160 kB
Created
Weekly downloads
 

Readme

Source

ejmorgan-retry

A simple and lightweight package to retry various execution contexts.

Installation

npm install ejmorgan-retry
// or
yarn add ejmorgan-retry

Usage

Basic

const { Retry } = require("ejmorgan-retry");

const retry = new Retry(function(resolve, reject, retry) {
    if (retry.attempts < 5)
        resolve(retry.reschedule(2000))
    else
        reject("oof!");
})

retry.schedule()
    .then((ok) => console.log(ok))
    .catch((err) => console.error(err));

HTTP Request

const https = require("https");
const { Retry } = require("ejmorgan-retry");

// create a Retry that wraps our execution context
const retry = new Retry((resolve, reject, retry) => {
  // let's make an http request
  const req = https.request({/* opts */}, (res) => {
    let data = "";

    res.on("data",(d) = > data += d);

    res.on("error", (e) => reject(e));

    res.on("end", () => {
      // success
      if (res.statusCode >= 200 && res.statusCode < 400) {
        resolve(res);
      } 
      
      // retry?
      else if (retry.attempts < 5) {
        // this line reschedules the retry
        // you MUST use the resolve() function
        // otherwise, using `return` or `reject`
        // will exit out of the Retry immediately
        resolve(retry.reschedule(2000));
      } 
      
      // error, no retries left
      else {
        reject("ERR");
      }
    });
  });

  req.end();
});

// let's call our wrapped execution context
retry.schedule()
  // handle resolve() or return
  .then((res) => console.log(res))
  // handle reject()
  .catch((err) => console.error(err));

FAQs

Last updated on 28 Nov 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc