@zcong/singleflight
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

singleflight

NPM version NPM downloads JS Test codecov

singleflight for js

Install

$ yarn add @zcong/singleflight
# or npm
$ npm i @zcong/singleflight --save

Example

import { Singleflight } from '../src'

const sf = new Singleflight()

const delay = (n: number) => new Promise((resolve) => setTimeout(resolve, n))

const fn = async () => {
  console.log('fn called')
  await delay(1000)
  return 'fn result'
}

const errFn = async () => {
  console.log('err fn called')
  await delay(2000)
  throw new Error('panic')
}

Array(10)
  .fill(null)
  .forEach(() => {
    sf.do('test', fn).then(console.log).catch(console.log)

    sf.do('test-err', errFn)
      .then(console.log)
      .catch((err) => console.log(`err: ${err.message}`))
  })

License

MIT © zcong1993

Readme

Keywords

none

Package Sidebar

Install

npm i @zcong/singleflight

Weekly Downloads

59

Version

0.3.1

License

MIT

Unpacked Size

8.32 kB

Total Files

6

Last publish

Collaborators

  • zcong