Giter VIP home page Giter VIP logo

Comments (10)

mikehall314 avatar mikehall314 commented on June 21, 2024

You should be able to pass a thisArg as the parameter to promisify. e.g. promisify(o.method, o);

from es6-promisify.

norswap avatar norswap commented on June 21, 2024

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.

mikehall314 avatar mikehall314 commented on June 21, 2024

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.

norswap avatar norswap commented on June 21, 2024

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.

mikehall314 avatar mikehall314 commented on June 21, 2024

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.

norswap avatar norswap commented on June 21, 2024

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.

mikehall314 avatar mikehall314 commented on June 21, 2024

As I say, if you want to submit a good PR I'd be happy to merge it in.

from es6-promisify.

norswap avatar norswap commented on June 21, 2024

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.

zhahaoyu avatar zhahaoyu commented on June 21, 2024

@norswap I think you can just do promisify(o.method).bind(o)

from es6-promisify.

dgoodua avatar dgoodua commented on June 21, 2024

https://github.com/digitaldesignlabs/es6-promisify/blob/7eb2f5e9ae858742d495978efebafaee6719da97/lib/promisify.js#L40

Change this line to
let target = this;

This must solve problem.

from es6-promisify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.