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

trier-promise

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trier-promise

A promise based trier

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

trier-promise

A promise based trier

Do one thing and do it well many times.

Installation

npm install trier-promise

Interface

Require

var tp = require('trier-promise');

Try something for 10 seconds at 1 second intervals

tp({
    action: someFunctionThatReturnsAPromise,
    timeout: 10000,
    interval: 1000
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'TIMEOUT'
  });

Try something for 10 seconds but guarantee 1 second sleep time between calls

If the asynchronous function takes a while, you may not want overlapping calls.

tp({
    action: aFunctionWithStdAsyncSignature,
    timeout: 10000,
    sleep: 1000,
    async: true   // note: this is needed so that tp knows to call with callback and wait
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'TIMEOUT'
  });

Try something 10 times, no matter the time

tp({
    action: aFunctionThatReturnsImmediately,
    limit: 10,
    sleep: 1000
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'LIMIT'
  });

Try something until some condition

tp({
    action: someFunctionThatReturnsAPromise,
    until: function() { return Math.random() < 0.1 },
    sleep: 1000
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'UNTIL'
  });

Exponentially growing interval

Sometimes you want to start polling rapidly but slow down for subsequent calls.

So instead of using a Number value as the interval (or sleep), specify a function that returns a Number.

var interval = 10;
tp({
    action: aFunctionWithStdAsyncSignature,
    timeout: 600000, // 10 minutes
    interval: function() { return interval *= 2; },
    async: true   // note: this is needed so that tp knows to call with callback and wait
  })
  .then(function(value) {
    // the value from the first success
  })
  .catch(function(error) {
    // error.message === 'TIMEOUT'
  });

Keywords

FAQs

Package last updated on 14 Oct 2016

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