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

Support promises and continuables in public APIs #92

Open
creationix opened this issue Mar 14, 2014 · 14 comments
Open

Support promises and continuables in public APIs #92

creationix opened this issue Mar 14, 2014 · 14 comments

Comments

@creationix
Copy link
Owner

The plan is to make the returned continuables also act like promises somehow, but not depending on a promise library. This will assume window.Promise exists and implements ES6 promises.

@ljharb
Copy link

ljharb commented Mar 14, 2014

It'd still be nice imo to fall back to a shim.

@creationix
Copy link
Owner Author

@ljharb what do you mean?

@ljharb
Copy link

ljharb commented Mar 14, 2014

If you rely on the existence of window.Promise, then only ES6 environments, or user-shimmed environments, will be able to benefit from the Promise API.

If instead you provide a Promise shim when a global implementation is unavailable, more environments (more people) will be able to benefit from the Promise API, which will then make them forwards-compatible as well :-)

@creationix
Copy link
Owner Author

I see. I really feel this is backwards though. If you want to consume js-git using ES6 promises, you have promises and already have the polyfill. If you want to consume js-git using node callbacks or continuables, then you don't want the bloat of a 2k minified polyfill included.

@creationix
Copy link
Owner Author

In other words. I'm willing to make js-git play nice in a promise environment, but it's outside the scope of js-git to provide such an environment if it doesn't exist already.

@Bartvds
Copy link

Bartvds commented Mar 14, 2014

If I may pitch in:

Some libraries allow users to pass their own promise constructor, or else fall back to a global defined Promise (or else bail with an error). So they don't ship any implementation at all.

This means user can pick either a light shim or a heavy implementation like bluebird, but without Promise.cast() etc.

@creationix
Copy link
Owner Author

@Bartvds that's essentially what I'm doing. I'm just using window.Promise as the entry point to pass in the promise implementation. I don't see this as an issue since I'm only planning to support ES6's Promise API. Is there ever a case where a user would have window.Promise but want to use a different Promise constructor in js-git?

@Bartvds
Copy link

Bartvds commented Mar 14, 2014

@creationix Using window.Promise is similar, but not quite the same as passing it in a call: global vars have a way of becoming problematic.

Simplest example: if you have an application with one module overwriting the global Promise with bluebird and another module who sets it to Q and then rely on their respective methods.

It is probably a bad idea to assume non-spec features about promises returned from a external module unless you can explicitly pass a constructor; any module might return an internal shim even with a global override set.

@ljharb
Copy link

ljharb commented Mar 14, 2014

@creationix a spec-compliant polyfill is tiny. See https://github.com/paulmillr/es6-shim/blob/master/es6-shim.js#L1065-L1410 - 350 lines is not going to be that large.

I'd hope that js-git would have a build process anyways, so I can always choose which parts I want to include and which not, so a Promise shim could just be omitted if I didn't want it.

@mariusGundersen
Copy link

What is the status on this? It would be nice to use with async/await now that it is getting supported, instead of using gen-run. I haven't looked into it, but couldn't there be a mixin that promisifies the api? Then nothing would be needed to be done to the core of js-git.

@creationix
Copy link
Owner Author

It probably wouldn't be hard to have a mixin that went through a set of known API signatures and turned them into promise based APIs. The harder task would be to automatically convery any api already mixed in.

@ljharb
Copy link

ljharb commented Feb 24, 2017

for the record: now, 3 years later, the best practice has indeed unequivocally become to just assume Promise is globally available and expect the user to shim it.

@mariusGundersen
Copy link

@creationix, I'm not familiar with the full scope of the api possible to create with mixins, but as long as they are enumerable methods on the repo object, then it shouldn't be too hard to loop through and promisify them. There are several npm packages that do this. I can try to have a look at this.

@creationix
Copy link
Owner Author

Go for it and let me know how it works.

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

4 participants