Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-promisable-result

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-promisable-result

A very small JavaScript utility to check and retry a function a limited number of times abstracting it in a Promise

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
37
decreased by-55.42%
Maintainers
0
Weekly downloads
 
Created
Source

get-promisable-result

A very small JavaScript utility to check and retry a function a limited number of times abstracting it in a Promise

Deployment Status Tests Coverage Status npm version

Install

npm
npm install get-promisable-result
yarn
yarn add get-promisable-result
PNPM
pnpm add get-promisable-result

API

getPromisableResult(
  getResultFunction: () => T,
  checkResult: (result: T) => boolean,
  options: PromisableOptions = {}
): Promise<T>

getResultFunction should be a function that returns something. This something is what you expect when the promise gets resolved.

checkResult is a function that will have as a parameter the result of the getResultFunction and it should return a boolean to validate that the result is OK. If this function returns false, the getResultFunction will be executed a limited number of times (consult the options section) until its result pass the check or until it reached the retries (consult the options section).

Options

ParameterTypeDefaultDescription
retriesnumber10number of retries that will be performed before the promise rejects
delaynumber10delay between the retries
shouldRejectbooleantrueindicates if the promise should be rejected when the number of retries reached the limit. If this parameter is set to false and the number of retries is reached the Promise will be resolved with the last value returned by getResultFunction
rejectMessagestring**custom error message. It can contain the substring {{ retries }} and it will be replaced with the number of retries

** The default rejectMessage will be Could not get the result after {{ retries }} retries

Example

Querying a DOM element that is rendered asynchronously with a custom retries, delay and rejectMessage

getPromisableResult(
  () => document.getElementById('my-element'),
  (element) => element !== null,
  {
    retries: 50,
    delay: 100,
    rejectMessage: 'My element could not be retrieved after {{ restries }} retries'
  }
)
  .then((element) => {
    // Do something with the element
  })
  .catch((error) => {
    // Do something if the promise is rejected
    // My element could not be retrieved after 50 retries
  });

Keywords

FAQs

Package last updated on 10 Nov 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc