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

try-run-js

v0.0.12

Published

An error code wrapper with a retry mechanism that supports passing in Promise or functions

Downloads

16

Readme

try-run-js

An error code wrapper with a retry mechanism that supports passing in Promise or functions

Install

npm install try-run-js

or

yarn install try-run-js

Usage

parameter

| parameter | desc | type | default | | :----------------- | :----------------- | :----------------- | :----------------------- | | promiseOrFun | promise or function | Function | Promsie | - | | options.retryTime | number of retries | number | 0 | | options.timeout | Timeout or timeout function (the parameter is the number of retries) | number |(time: number) => number | time: number) => Promise | 333 |

import tryRun, { tryRunForTuple } from "try-run-js";

const { result } = await tryRun(Promise.resolve(12));
// => result 12

const { error } = await tryRun(Promise.reject(12));
// => error 12

// The first is the error, the second is the result
const [_error, tupleResult] = await tryRunForTuple(Promise.resolve(12));
// => tupleResult 12

const [tupleError, _tupleResult] = await tryRunForTuple(Promise.reject(12));
// => tupleError 12
let index = 0;
const { result } = await tryRun<any>(() => {
  index++;
  // The result will be returned on the third run
  if (index === 3) {
    return Promise.resolve(222);
  }
  return Promise.reject(12);
}, {
  retryTime: 3,
});
// 222

const options = {
  // retry 3 times
  retryTime: 3,
  // Retry after every 3s
  timeout: 3000,
};

// It is also possible to pass functions
options.timeout = (time: number) => {
  // The current number of retries * 1000
  // 1000 the first time, 2000 the second time, and so on
  return time * 1000;
};

// You can also use async functions
options.timeout =  (time: number) => {
  return new Promise(resolve => [
    requestAnimationFrame(() => {
      resolve()
    })
  ])
};

const { result: styleResult, error } = await tryRun(() => {
  const dom = document.getElementById("ppt");
  // If the dom node does not exist, an error will be reported at this time, and the function has an error retry mechanism
  return dom.style;
}, options);

if (error) {
  // Handling error triggers ...
  return;
}

// get style result
styleResult;

Changelog

  • 0.0.12 Fix default value error
  • 0.0.11 Code optimization
  • 0.0.10 The return value of the tryRun function is set to null on error
  • 0.0.9 Add timeout to return Promise
  • 0.0.8 Remove the return type setting and separate it into two functions tryRun and tryRunForTuple
  • 0.0.7 Fixed a bug that would also wait after the last retry
  • 0.0.5 complete docs
  • 0.0.4 To complete the basic functions, the function can be used in places that may go wrong and need to be retried