Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ngryman/p-using
Dispose a resource when p-using promise resolves.
https://github.com/ngryman/p-using
Last synced: about 2 months ago
JSON representation
Dispose a resource when p-using promise resolves.
- Host: GitHub
- URL: https://github.com/ngryman/p-using
- Owner: ngryman
- License: mit
- Created: 2017-10-14T14:56:56.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-29T03:52:59.000Z (over 1 year ago)
- Last Synced: 2024-07-26T13:38:38.917Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 1.14 MB
- Stars: 1
- Watchers: 3
- Forks: 3
- Open Issues: 18
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# p-using [![travis][travis-image]][travis-url] [![codecov][codecov-image]][codecov-url] [![size][size-image]][size-url]
> Dispose a resource when p-using resolves.
[travis-image]: https://img.shields.io/travis/ngryman/p-using.svg?style=flat
[travis-url]: https://travis-ci.org/ngryman/p-using
[codecov-image]: https://img.shields.io/codecov/c/github/ngryman/p-using.svg
[codecov-url]: https://codecov.io/github/ngryman/p-using
[size-image]: http://img.badgesize.io/https://unpkg.com/[email protected]/dist/p-using.min.js?compression=gzip
[size-url]: https://unpkg.com/[email protected]/dist/p-using.min.js
Inspired from [Bluebird](http://bluebirdjs.com/docs/api/promise.using.html), `p-using` makes sure that the specifed `dispose` function is called on a `resource`.
## Installation
```sh
$ npm install --save p-using
```## Usage
```js
import using from 'p-using'// call `db.dispose` method
using(db, db => { db.query('...') })// call `db.close` method
using(db, db => { db.query('...') }, 'close')// call the provided dispose function
using(db, db => { db.query('...') }, db => { db.finalize() })// accept a resource promise
using(createDb(), db => { db.query('...') })
```## API
### using
**Parameters**
- `resource` **([Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) \| [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise))** Resource to be disposed.
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to be invoked before disposal.
- `disposeFunction` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function))** Name of the method or function called for disposal.Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** Promise resolved when the resource has been disposed.
## License
MIT © [Nicolas Gryman](http://ngryman.sh)