feature: all bind-all when passed an object
kumavis opened this issue · comments
kumavis commented
pify has a nice feature where you can promisify an obj's methods
example from readme:
// Promisify all methods in a module
pify(fs).readFile('package.json', 'utf8').then(data => {
console.log(JSON.parse(data).name);
//=> 'pify'
});
however this doesnt work when the child methods expect a reference to the owning object
example from my experience:
const Client = require('coinbase').Client
const pify = require('pify')
const client = pify(new Client({}))
const accounts = await client.getAccounts(null) // fails due to unset `this` reference
work around - use bind-all
:
const Client = require('coinbase').Client
const pify = require('pify')
const bindAll = require('bind-all')
const client = pify(bindAll(new Client({})))
const accounts = await client.getAccounts(null)
Sindre Sorhus commented
Fixed by #73.