Comments (10)
You should be able to pass a thisArg as the parameter to promisify. e.g. promisify(o.method, o);
from es6-promisify.
I know, this is what I meant by "binds the receiver explicitly". Whenever you use the function returned by promisify, this
will always be o
inside the method.
What I'm doing is something like this:
MyClass.prototype.method_promise = promisify_unbound(MyClass.prototype.method)
...
instance1.method_promise(...) // this = instance1 inside the method
instance2.method_promise(...) // this = instance2 inside the method
from es6-promisify.
Ah, I see. Do you think this is a good use case for a promisify library? Would it not be simpler to just return a promise?
from es6-promisify.
I meant to use it on methods that belong to libraries, which I did not write. Seems like a pretty good use case to me. (So MyClass
is a bad name -- it should be LibraryClass
.)
from es6-promisify.
I'm unconvinced of the use case for this. If you're modifying the prototype of a library, I'm not sure why you wouldn't do something like:
LibraryClass.prototype.method_promise = function (...args) {
return new Promise((resolve, reject) => {
args.push((err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
this.method(...args);
});
};
or even:
LibraryClass.prototype.method_promise = function (...args) {
if (!this._method_promise) {
this._method_promise = promisify(this.method, this);
}
return this._method_promise(...args);
};
But if you want to submit a PR, I'd be happy to merge it :)
from es6-promisify.
By the same token, you can always promisify a regular function by doing:
fun promisified(...args) {
return new Promise(function (resolve, reject) {
libFunction(...args, function(err, data) {
if (err == undefined) resolve(data)
else reject(err)
})
})
}
Isn't the whole point of the library to do this as a one-liner,
and by the same token, shouldn't it also be a one-liner for methods?
from es6-promisify.
As I say, if you want to submit a good PR I'd be happy to merge it in.
from es6-promisify.
I'll do it.
A question regarding the interface however: my preference would be to drop the support for binding (the promisify(o.method, o)
syntax). The unbinding way is more general and if you want binding it's very easy:
promisify(o.method).bind(o)
// or equivalently
promisify(o.method.bind(o))
Is that okay?
from es6-promisify.
@norswap I think you can just do promisify(o.method).bind(o)
from es6-promisify.
Change this line to
let target = this;
This must solve problem.
from es6-promisify.
Related Issues (20)
- Promises with multiple arguments group args into an array HOT 3
- Remove "own resolver" functionality HOT 1
- Document a callback with multiple arguments HOT 7
- Should detect attempts to promisify promises
- Is it possible to profimify a function in the format of func(arg1, arg2 .. argN, {callback: Function}) HOT 2
- Minify bundle. HOT 2
- Bundling a promise polyfill is not optimal HOT 1
- Add `thisArg` option usage in readme documentation HOT 1
- Does not work on iOS9 safari HOT 1
- `multiArgs` doesn't work in case of rejection HOT 4
- Invalid assignment error.
- Please add LICENSE file HOT 1
- This library should not require es6-promise,may be I require another promise library. HOT 1
- is it possible to trigger HTTP requests from this plugin? HOT 1
- Latest version only returns first callback value if argumentNames is not set HOT 2
- Deploy on Azure failed
- Unable to promisify localStorage HOT 1
- Does this work with async and await? HOT 2
- Cannot read property 'apply' of undefined HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from es6-promisify.