Skip to content

feature: all bind-all when passed an object #48

@kumavis

Description

@kumavis

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions