Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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)