https://github.com/gulpjs/async-once
Guarantee a node-style async function is only executed once.
https://github.com/gulpjs/async-once
Last synced: 8 months ago
JSON representation
Guarantee a node-style async function is only executed once.
- Host: GitHub
- URL: https://github.com/gulpjs/async-once
- Owner: gulpjs
- License: mit
- Created: 2015-03-01T02:46:33.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-09-03T23:42:56.000Z (almost 3 years ago)
- Last Synced: 2024-10-29T15:14:38.318Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 8
- Watchers: 6
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# async-once
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coveralls Status][coveralls-image]][coveralls-url]
Guarantee a node-style async function is only executed once.
## Usage
```js
var once = require('async-once');
var count = 0;
var myAsyncFunc = once(function (cb) {
count++;
cb(null, count);
});
myAsyncFunc(function (err, result) {
assert(result === 1);
});
myAsyncFunc(function (err, result) {
assert(result === 1);
});
assert(count === 1);
```
## API
### `once(fn)`
Takes a node-style async function (`fn`) to ensure it's only called once. The function should accept a callback as its last parameter which is called with `cb(err, result)`. Returns a function that can be called any number of times but will only execute once. Arguments passed to the returned function will be passed to the `fn`.
## License
MIT
[downloads-image]: https://img.shields.io/npm/dm/async-once.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/async-once
[npm-image]: https://img.shields.io/npm/v/async-once.svg?style=flat-square
[ci-url]: https://github.com/gulpjs/async-once/actions?query=workflow:dev
[ci-image]: https://img.shields.io/github/actions/workflow/status/gulpjs/async-once/dev.yml?branch=master&style=flat-square
[coveralls-url]: https://coveralls.io/r/gulpjs/async-once
[coveralls-image]: https://img.shields.io/coveralls/gulpjs/async-once/master.svg?style=flat-square