Josh Klodnicki
Josh Klodnicki

Reputation: 625

Is there a "best" way to promisify and call a method function?

The way I see it, there are three ways to promisify and call a method function in one shot:

const { promisify } = require('util');

const obj = {
    method: function (param, callback) {
        // Do something async with `this`
        callback(err, result);
    }
};

promisify(obj.method.bind(obj))(arg).then(doSomething); // {1}
promisify(obj.method).bind(obj)(arg).then(doSomething); // {2}
promisify(obj.method).call(obj, arg).then(doSomething); // {3}

Are any of these methods preferred over the others, or are they all equally valid? What are the advantages and disadvantages of each?

Upvotes: 2

Views: 591

Answers (0)

Related Questions