https://github.com/zcong1993/singleflight
singleflight for js
https://github.com/zcong1993/singleflight
Last synced: 7 months ago
JSON representation
singleflight for js
- Host: GitHub
- URL: https://github.com/zcong1993/singleflight
- Owner: zcong1993
- License: mit
- Created: 2019-07-02T03:40:53.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-01-13T17:31:16.000Z (9 months ago)
- Last Synced: 2025-04-07T08:15:48.714Z (7 months ago)
- Language: TypeScript
- Homepage:
- Size: 403 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# singleflight
[](https://npmjs.com/package/@zcong/singleflight) [](https://npmjs.com/package/@zcong/singleflight) [](https://github.com/zcong1993/singleflight/actions/workflows/js-test.yml) [](https://codecov.io/gh/zcong1993/singleflight)
> singleflight for js
## Install
```sh
$ yarn add @zcong/singleflight
# or npm
$ npm i @zcong/singleflight --save
```
## Example
```ts
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