sindresorhus / pify

Promisify a callback-style function

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feature: all bind-all when passed an object

kumavis opened this issue · comments

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)

Fixed by #73.