Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods for keeping this #23

Closed
LinusU opened this issue May 9, 2016 · 9 comments
Closed

Methods for keeping this #23

LinusU opened this issue May 9, 2016 · 9 comments

Comments

@LinusU
Copy link
Contributor

LinusU commented May 9, 2016

I find that I quite often need to preserve this when calling functions that takes callbacks. I have used something similar to the code below, but I would like to hear what you think of the matter.

Is there an easy way to support this use case?

function promise (obj, fn) {
  return pify(obj[fn]).bind(obj)
}

await promise(linus, 'walk')(100, 'miles')
console.log('Linus walked 100 miles')

// vs.

linus.walk(100, 'miles', () => console.log('Linus walked 100 miles'))
@SamVerschueren
Copy link
Contributor

Just use bind directly.

await pify(linus.walk.bind(linux))(100, 'miles');

@LinusU
Copy link
Contributor Author

LinusU commented May 9, 2016

That's true, I guess that it would be even nicer if pify supported it out of the box though. Maybe it's outside the scope though...

await pify(linus, 'walk')(100, 'miles')

@sindresorhus
Copy link
Owner

@LinusU How would it know what to bind to? You would have had to pass the this in anyways.

@SamVerschueren
Copy link
Contributor

SamVerschueren commented May 9, 2016

@sindresorhus He wants that this example

await pify(linus, 'walk')(100, 'miles')

binds the walk method to the linus object and thus to itself. But this was discussed in the past if I'm not mistaken.

@sindresorhus
Copy link
Owner

Yeah, not worth saving just a few characters. Using bind is a lot clearer too.

@LinusU
Copy link
Contributor Author

LinusU commented May 9, 2016

Yes, what @SamVerschueren said, I should have been more clear.

I don't know if it's the cleanest approach, my main goal is just to keep the code clean and readable. I guess that we are at a point where many libraries are transitioning into promises, but it will probably be a long time until we are all the way there.

The problem I see with doing, e.g. pify(fs.readFile) is that I make an assumption that readFile doesn't use this. It seems like this will creep up to bite me at some point, which is why I would prefer to solve it somehow...

The function bind operator could solve this by using pify(::fs.readFile), but that isn't nowhere near ready though :)

@SamVerschueren
Copy link
Contributor

With using bind, you can bind the method to whatever you want so it's much more flexible and it's clear for everyone that it is bound to the object you pass in.

I make an assumption that readFile doesn't use this. It seems like this will creep up to bite me at some point.

Your tests should catch that.

@sindresorhus
Copy link
Owner

I've rarely encountered the need to use bind though. Most API's in the Node.js world are luckily not using this.

@LinusU
Copy link
Contributor Author

LinusU commented May 9, 2016

I've rarely encountered the need to use bind though. Most API's in the Node.js world are luckily not using this.

True, I guess that this is good enough for now 👍 thanks for the discussion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants