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

promise-effects

v2.1.0

Published

Promise effects for advanced use cases like retry, timeout, observation over promises.

Downloads

3

Readme

Promise Effects

npm npm

Fully typed promise effects to play with async handlers in a pleasent way.

| Effect | Desc | |-----------------------------------|-------------------------------------------------------| | retryPromise | Converts a promise creator to repetitive | | wait | Creates a promise to resolve for the given time long. | | observePromise | Creates observable promises from the given promise | | timeout | Timeout example | | Polling | ⏱ Polling example | | Latest | ⏱ | | CancelablePromise | ⏱ |

Usage

Install package

npm install promise-effects

Import module

import { retryPromise } from 'promise-effects'
// or
import retryPromise from 'promise-effects/retryPromise'

Effects

RetryPromise

It's a high-order function that gives the capabilify of retry to a function that creates promise.

const repetitiveFetch = retryPromise(fetch, {
  retry: 3,
  onReconnecting: ({ attemptNumber }) => console.log(`Fetching has failed(${attemptNumber})... Retrying...`),
})
repetitiveFetch('https://www.mocky.io/v2/5185415ba171ea3a00704eed')
  .then(resp=> resp.json())
  .then(console.log)
  .catch(() => console.log('Failed!'))
// > Fetching has failed(1)... Retrying...
// > Fetching has failed(2)... Retrying...
// > Fetching has failed(3)... Retrying...
// > Failed!

Options

| Option Name | Type | |----------------|---------------------------------------------------------------------------------------------| | onReconnecting | fn:({attemptNumber: number, remainingTries: number, error: Error}) | | delay | number | | delay | { delay: number, factor: number, max?: number, min?: number } | | delay | fn: ({attemptNumber: number, remainingTries: number, error: Error}) => number | Promise | | shouldRetry | fn: ({attemptNumber: number, remainingTries: number, error: Error}) => booleaan |Promise<boolean> | | retry | number |

observePromise

It wraps promises to observe the status. You can get if the promise fulfilled, rejected or pending. It's beneficial on test purposes.

const observed = observePromise(fetch('http://google.com'))
console.log(observed.isPending()) // > true
console.log(observed.isRejected()) // > false
console.log(observed.isFulfilled()) // > false
console.log(observed.status) // > 'PENDING'
observed
  .then(result=> {
    console.log(observed.status) // > 'RESOLVED'
    console.log(observed.isFulfilled()) // > true
  })
  .catch(() => {
    console.log(observed.status) // > 'REJECTED'
    console.log(observed.isRejected()) // > true
  })

wait

Creates a promise to resolve for the given time long.

const printMessage = (count=0) =>
  Promise.resolve()
    .then(() => console.log(`You will see this message every second(${count})`))
    .then(() => wait(1000).then(() => printMessage(count + 1)))

printMessage()
// > You will see this message every second(0)
// > You will see this message every second(1)
// > You will see this message every second(2)
// > You will see this message every second(3)
// > ...

timeout

You don't need an exclusive timeout function for timeout. Use wait with ES Promise.race instead.

  // fail in 1 sec
  Promise.race([
    fetch('https://www.mocky.io/v2/5185415ba171ea3a00704eed?mocky-delay=1200ms').then(res=> resp.json()),
    wait(1000).then(()=> Promise.reject('Timeout!'))
  ])
  // > Uncaught (in promise) Timeout!
  // will mock response if the first request doesn't resolve in 1 sec
  const result = await Promise.race([
    fetch('https://www.mocky.io/v2/5185415ba171ea3a00704eed?mocky-delay=1200ms').then(res=> resp.json()),
    wait(1000).then(()=> ({ hello: 'mock' }))
  ])
  console.log(result)
  // > { hello: 'mock' }

Changelog

All notable changes to this project will be documented in the changelog file.