Git Product home page Git Product logo

promise-utils's Introduction

promise-utils

A few tools about promise, like delay, map, filter and retry.

npm version build status install size npm downloads

Install

$ npm install @shanyue/promise-utils

Require node > 10.x

Usage

import { map, filter, retry, sleep } from '@shanyue/promise-utils'


// Output: [2, 3, 4, 5]
await map([Promise.resolve(1), 2, 3, 4], (x: number) => x + 1)

// Output: [1, 3]
await filter([Promise.resolve(1), 2, 3, 4], (x: number) => Boolean(x % 2))

await sleep(300)

await retry(() => Promise.resolve(3), { times: 3 })

API

map(input, mapper, [options])

Map promises concurrently

  • input: Promise or any value
  • mapper: Expected to return a Promise or value.
  • options.concurrency: Number of concurrently pending promises.
import { map } from '@shanyue/promise-utils'

// Output: [2, 3, 4, 5]
await map([Promise.resolve(1), 2, 3, 4], (x: number) => x + 1)

// Output: [2, 3, 4, 5]
await map([Promise.resolve(1), 2, 3, 4], (x: number) => Promise.resolve(x + 1))

// After 4 * 300ms,Output: [2, 3, 4, 5]
await map([1, 2, 3, 4], async x => {
  await sleep(300)
  return x + 1
}, { concurrency: 1 })

filter(input, filterer, [options])

Filter promises concurrently

  • input: Promise or any value.
  • filterer: Expected to return boolean-like value.
  • options.concurrency: Number of concurrently pending promises.
import { filter } from '@shanyue/promise-utils'

// Output: [1, 3]
await filter([Promise.resolve(1), 2, 3, 4], (x: any) => Boolean(x % 2))

// Output: [1, 3]
await filter([Promise.resolve(1), 2, 3, 4], (x: any) => Promise.resolve(Boolean(x % 2)))

retry(input, [options])

Retry promise

  • input: Promise or any value.
  • options.times: Number of concurrently pending promises.
  • options.attemptNumber: Number of concurrently pending promises.
import { retry, AbortError } from '@shanyue/promise-utils'

// Output: 'ok'
await retry(async attemptNumber => {
  return attemptNumber === 3 ? 'ok' : Promise.reject(new Error('error'))
}, {
  times: 3
})

// Output: 'ok'
await retry(async attemptNumber => {
  return attemptNumber === 3 ? 'ok' : Promise.reject(new Error('error'))
}, {
  times: 3,
  onFailedAttempt: async (e) => {
    console.log(e)
    // retry delay 1s
    await sleep(1000)
  }
})


// AbortError fail-fast
try {
  await retry(async () => {
    const err = new AbortError('hello')
    return Promise.reject(err)
  }, {
    times: 3
  })
} catch (e) {
}

sleep(ms)

import { retry } from '@shanyue/promise-utils'

// delay 1s
await sleep(1000)

promise-utils's People

Contributors

dependabot[bot] avatar shfshanyue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.