Skip to content

Latest commit

 

History

History
76 lines (61 loc) · 1.62 KB

README.md

File metadata and controls

76 lines (61 loc) · 1.62 KB

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));