-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
Allow promisification of a single object method: #26
Conversation
It was discusses a couple of times in other issues #23. But we can always reopen the discussion :). I might find the first example clearer though. |
There's a nice ES proposal for a return pify(
browserify()
.require(file)
.transform(brfs)
.exclued(excluded)::bundle
)(); respectively: return pify(
browserify()
.require(file)
.transform(brfs)
.exclued(excluded)
)::bundle(); |
@@ -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); |
There was a problem hiding this comment.
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);
The The example from #23 doesn't illustrate situations where extracting the |
What if you want to use |
How about a return pify(
browserify()
.require(file)
.transform(brfs)
.excluded(excluded),
{bind: true}).bundle(); It could accept |
@jamestalmage If you are curious about, |
Not true, they will be interpreted as function bar() {
console.log(this.foo + 'bar1');
}
var foo = {
foo: 'foo',
bar: function () {
console.log(this.foo + 'bar2');
}
};
setImmediate(foo::bar);
//=> foobar1;
setImmediate(::foo.bar);
//=> foobar2;
I don't see a whole lot of point to var obj = {
coreMethod(cb) {
// does something before calling cb
},
wrapperMethod(cb) {
// `this` needs to be the original once promisified, or we will call the promisified method here
this.coreMethod(function(err, result) {
if (err) {
cb(err);
return;
}
// do some more stuff before calling cb
}
}
} |
It could be set to |
@jamestalmage Thank you, you're right, good catch 👍
IMO, var legacyMixin = {
bars: function (cb) {
cb(null, this.foo + ' bars');
}
};
class C {
constructor(foo) {
this.foo = foo;
}
foobar() {
return Promise.resolve('foobar');
}
}
Object.assign(C.prototype, pify(legacyMixin, {bind: false}));
(new C(42)).bars().then(console.log);
(new C(42)).foobar().then(console.log); |
Closing in favor of #27. |
This allows
opts
to be astring
. If so, it will promisify the named function ofinput
and return a thunk withthis
bound toinput
.This is really handy for chainable structures, where binding
this
can get a little verbose:becomes