Awesome Open Source
Awesome Open Source

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


Install package

npm install promise-effects

Import module

import { retryPromise } from 'promise-effects'
// or
import retryPromise from 'promise-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...`),
  .then(resp=> resp.json())
  .catch(() => console.log('Failed!'))
// > Fetching has failed(1)... Retrying...
// > Fetching has failed(2)... Retrying...
// > Fetching has failed(3)... Retrying...
// > Failed!


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
shouldRetry `fn: ({attemptNumber: number, remainingTries: number, error: Error}) => booleaan
retry number


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(''))
console.log(observed.isPending()) // > true
console.log(observed.isRejected()) // > false
console.log(observed.isFulfilled()) // > false
console.log(observed.status) // > 'PENDING'
  .then(result=> {
    console.log(observed.status) // > 'RESOLVED'
    console.log(observed.isFulfilled()) // > true
  .catch(() => {
    console.log(observed.status) // > 'REJECTED'
    console.log(observed.isRejected()) // > true


Creates a promise to resolve for the given time long.

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

// > 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)
// > ...


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

  // fail in 1 sec
    fetch('').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('').then(res=> resp.json()),
    wait(1000).then(()=> ({ hello: 'mock' }))
  // > { hello: 'mock' }


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

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Typescript (253,778
Async (3,156
Promise (1,432
Retry (251
Polling (142
Timeout (139
Related Projects