Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow promisification of a single object method: #26

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var pify = module.exports = function (obj, P, opts) {
P = Promise;
}

if (typeof opts === 'string') {
return processFn(obj[opts], P, {}).bind(obj);
Copy link
Contributor

@schnittstabil schnittstabil Jun 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the intention of pify(x, 'foo') isn't obvious, should pify bind the promisified foo function to the non-promisified or the (new) promisified object – both are reasonable, at least sometimes.

return processFn(obj[opts], P, {}).bind(obj);
// vs.
var objP = processFn(obj, P, {});
return objP[opts].bind(objP);

}

opts = opts || {};
var exclude = opts.exclude || [/.+Sync$/];

Expand Down
2 changes: 1 addition & 1 deletion optimization-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-fallthrough */
/* eslint-disable no-fallthrough, xo/no-process-exit*/
'use strict';
var assert = require('assert');
var Promise = require('pinkie-promise');
Expand Down
20 changes: 19 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pify(fs).readFile('package.json', 'utf8').then(data => {

## API

### pify(input, [promiseModule], [options])
### pify(input, [promiseModule], [methodName | options])

Returns a promise wrapped version of the supplied function or module.

Expand All @@ -52,6 +52,24 @@ Custom promise module to use instead of the native one.

Check out [`pinkie-promise`](https://github.com/floatdrop/pinkie-promise) if you need a tiny promise polyfill.

#### methodName

Type: `string`

Specify a specific member function of `input` to promisify:

```js
var x = {
a: 'foo',
b: function (b, cb) {
cb(null, this.a + b);
}
};

pify(x, 'b')('bar')
//=> Promise for "foobar"
```

#### options

##### multiArgs
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ const fixtureModule = {
method3: fixture5
};

class Concat {
constructor(a) {
this.a = a;
}

b(b, cb) {
setImmediate(() => cb(null, this.a + b));
}
}

test('main', async t => {
t.is(typeof fn(fixture)().then, 'function');
t.is(await fn(fixture)(), 'unicorn');
Expand All @@ -44,6 +54,12 @@ test('multiArgs option', async t => {
t.deepEqual(await fn(fixture3, {multiArgs: true})(), ['unicorn', 'rainbow']);
});

test('string option returns promisified thunk for member function', async t => {
const concat = new Concat('foo');

t.is(await fn(concat, 'b')('bar'), 'foobar');
});

test('wrap core method', async t => {
t.is(JSON.parse(await fn(fs.readFile)('package.json')).name, 'pify');
});
Expand Down