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

promise-assist

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-assist

Several helper functions when working with native promises

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.7K
increased by53.61%
Maintainers
1
Weekly downloads
 
Created
Source

promise-assist

Build Status npm version

Several helper functions when working with native promises.

API

sleep

Useful for waiting a specific amount of time before continuing an operation.

import { sleep } from 'promise-assist'

async function myOperation() {
    const startTime = Date.now()
    await sleep(500)
    console.log(`${Date.now() - startTime}ms passed!`)
}

timeout

Useful for limiting the amount of time an async Promise-based operation can take.

import { timeout } from 'promise-assist'

async function myOperation() {
    try {
        const data = await timeout(
            fetchDataFromServer(), // pass a Promise to the timeout function
            10000, // request will be limited to 10 seconds
            `failed loading required data from backend`
        )
        // do something with the data
    } catch (e) {
        // handle errors
    }
}

deferred

Creates a deferred Promise, where resolve/reject are exposed to the place that holds the promise.

Generally a bad practice, but there are use-cases, such as mixing callback-based and Promise-based APIs, where this is helpful.

import { deferred } from 'promise-assist'

const { promise, resolve, reject } = deferred<string>()

// `resolve` or `reject` calls are reflected on `promise`
promise.then(value => console.log(value))
resolve('some text')
// 'some text' is printed to console

retry

Executes provided action (sync or async) and returns its value. If action throws or rejects, it will retry execution several times before failing.

Defaults are:

  • 3 retries
  • no delay between retries
  • no timeout to stop trying

These can be customized via a second optional options parameter.

import { retry } from 'promise-assist'

// with default options
retry(() => fetch('http://some-url/asset.json'))
    .then(value => value.json())
    .then(console.log)
    .catch(e => console.error(e))

// with custom options
retry(() => fetch('http://some-url/asset.json'), {
        retries: Infinity,         // infinite number of retries
        delay: 10 * 1000,          // 10 seconds delay between retries
        timeout: 2 * 60 * 1000     // 2 minutes timeout to stop trying
    })
    .then(value => value.json())
    .then(console.log)
    .catch(e => console.error(e))

License

MIT

FAQs

Package last updated on 15 Oct 2018

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