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
Unbound methods, interest in autobinding? #345
Comments
While the issue you document will currently occur–Javascript in general behaves in this manner–is there a reason to have everything bound by default? Could you elaborate on your use case for having methods bound? Why not just pass around the objects that the library returns to you? |
The reason for binding methods on the returned objects is purely to provide a more convenient developer interface. An example use case would be e.g. conversion to promise based / async-await control flows: import denodeify from 'denodeify';
import Github from 'github-api';
const github = new Github();
const getRepo = denodeify(github.getRepo);
async function main() {
const repo = await getRepo();
return repo;
}
main()
.then()
.catch(error => {
// barfs with:
// => TypeError: Cannot read property '_request' of undefined
}); This is fixed with a simple |
Why not: import Github from 'github-api';
const github = new Github();
async function main() {
const repo = await github.getRepo('michael', 'github');
return repo;
}
main()
.then(repoJson => {
// do stuff
}).catch(error => {
}); You're going to need to pass things around (whether they're functions or the I guess to answer the question you asked I don't think that this is something we'd like to support by default. However, if you submit a PR that enables this behind a flag or option upon instantiation then I guess I don't have a problem with it. It just doesn't feel very javascript-y. See React's reasoning for dropping support for auto-bound methods like this as they transition to ES6+ usage. |
Thanks for the clarification and fast response. Makes a lot of sense to me. 👍 |
I noticed methods on all objects are unbound, e.g.
Of course this can be fixed easily by changing it to:
This is rather clumsy and moves the work to bind the methods to their respective host objects to users although it could easily be done by the library.
The text was updated successfully, but these errors were encountered: