Sha256: df2bc9a7c8d866538157436f9d58a48d6bffc756913fa99cbb2578723519ecea

Contents?: true

Size: 1.44 KB

Versions: 40

Compression:

Stored size: 1.44 KB

Contents

# @gar/promisify

### Promisify an entire object or class instance

This module leverages es6 Proxy and Reflect to promisify every function in an
object or class instance.

It assumes the callback that the function is expecting is the last
parameter, and that it is an error-first callback with only one value,
i.e. `(err, value) => ...`. This mirrors node's `util.promisify` method.

In order that you can use it as a one-stop-shop for all your promisify
needs, you can also pass it a function.  That function will be
promisified as normal using node's built-in `util.promisify` method.

[node's custom promisified
functions](https://nodejs.org/api/util.html#util_custom_promisified_functions)
will also be mirrored, further allowing this to be a drop-in replacement
for the built-in `util.promisify`.

### Examples

Promisify an entire object

```javascript

const promisify = require('@gar/promisify')

class Foo {
  constructor (attr) {
    this.attr = attr
  }

  double (input, cb) {
    cb(null, input * 2)
  }

const foo = new Foo('baz')
const promisified = promisify(foo)

console.log(promisified.attr)
console.log(await promisified.double(1024))
```

Promisify a function

```javascript

const promisify = require('@gar/promisify')

function foo (a, cb) {
  if (a !== 'bad') {
    return cb(null, 'ok')
  }
  return cb('not ok')
}

const promisified = promisify(foo)

// This will resolve to 'ok'
promisified('good')

// this will reject
promisified('bad')
```

Version data entries

40 entries across 40 versions & 2 rubygems

Version Path
immosquare-cleaner-0.1.60 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.59 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.58 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.57 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.56 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.55 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.54 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.53 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.52 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.51 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.50 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.49 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.48 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.47 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.46 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.45 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.44 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.43 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.42 node_modules/@gar/promisify/README.md
immosquare-cleaner-0.1.41 node_modules/@gar/promisify/README.md