https://github.com/autonomoussoftware/promise-limit-one
Wrap a function so if called multiple times, only one call runs simultaneously
https://github.com/autonomoussoftware/promise-limit-one
Last synced: 4 months ago
JSON representation
Wrap a function so if called multiple times, only one call runs simultaneously
- Host: GitHub
- URL: https://github.com/autonomoussoftware/promise-limit-one
- Owner: autonomoussoftware
- License: mit
- Created: 2019-02-22T19:13:41.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-06T17:48:18.000Z (over 4 years ago)
- Last Synced: 2025-04-08T10:52:12.563Z (10 months ago)
- Language: JavaScript
- Size: 44.9 KB
- Stars: 2
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# promise-limit-one
[](https://travis-ci.com/autonomoussoftware/promise-limit-one)
Wrap a function so if called multiple times, only one call runs simultaneously.
*Note*: This package replaces `promise-not-if-busy`, which is now deprecated.
## Installation
```shell
npm install promise-limit-one
```
## Usage
```js
const limitOne = require('promise-limit-one')
const longOperation = () => new Promise(function (resolve) {
console.log('Executing a long operation...')
setTimeout(resolve, 100)
})
const wrapped = limitOne(longOperation)
wrapped() // will print "Executing..."
wrapped() // will do nothing
setTimeout(function () {
wrapped() // will print "Executing..." again
}, 150)
```
## API
### `limitOne(fn, err)`
Creates a function that when called multiple times will call `fn` and wait for it to finish before calling `fn` again.
If `err` is supplied and the function is running, any new call will reject with `err`.
#### Params
`fn`:
Function that returns a promise.
#### Returns
A function that wraps `fn` up.
It will return a promise that will resolve to the result of calling `fn`.
While `fn` is busy, it will resolve to the pending promise.
## License
MIT