-
Notifications
You must be signed in to change notification settings - Fork 24
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 for Promises? #49
Comments
I'm not opposed to this; promises are interesting and it'll be interesting to see what comes of DOM/ES6 support for them. With The "easy" way is to just provide a signal value |
Oh, I hadn't thought about that at all. The only alternative I can think of other than having a promise adapter like |
So here's a question for you: on the 'upcoming' branch I've got some code to support streams and better control over response types. I guess at this point it's still an experiment, but right now I just tacked on a fourth argument so it's like Anyway, all this to say, if this experiment ships (xref #51) I suppose we could also support 'promise' as a response type (and detect it as a request data type too) like working with streams. Basically the way I see this getting enabled is sort of similar to how Proxy is handled [and/or needs to start getting handled in the next release]:
Unlike with Proxy though, I'm guessing there's several (if not dozens…) of potential promise standins, but hopefully one stands out as the simplest/sanest/correctest to choose as the optional Dependency, and then I'm assuming it should just interop even if people want to use a different Promise polyfill in their own code? [Also paging @calvinmetcalf, would love your feedback on this too!] |
Looking at some other http client libraries that utilize promises, most appear to specify a hard dependency on some choice es6-compliant promise library, if the current platform lacks promise support. The main problem I see with such a method is that it's impossible for commonjs bundlers to predict at compilation time whether each and every host environment will have promise support (eg. older browsers and non-harmony node), and thus what is deemed as an "optional" promise polyfill ends up becoming dead weight when there actually exists built-in support. This only becomes confounded when working with multiple promise-based libraries, as the resulting built files will contain multiple (arguably redundant) promise implementations. With that in mind, I have no real robust solution for allowing proper promise interop. Seeing as fermata already works without promises, perhaps it could error unless a promise implementation is provided through some hook (ie. |
Promises are all interoperable so don't worry about that, for what type if it's node only then I'd recommend bluebird which is super fast but super large, for the browser I'd recommend my own project lie. |
Thanks, that's helpful for that side of things! On the other side, I'm realizing I need to revisit the API for this and #33 though — the whole point of promises (and streams) is not having to wait for a callback. For streams it makes a little sense, but it'd be 100% silly to have to still give fermata a callback to receive a promise…the way 'request' kind of magically handles streaming is pleasant and that's more like what we'd need for promises (and streams then too). |
I promise to use Fermata if you support promises! |
Any update on this? |
@legomind Not really, I'm hoping to polish up and release the
I do think promises are kind of nifty, but to be honest I'm not sold on them either. They appear only a few places in new browser API, a few libraries like jQuery sort of use them a little, while node.js itself doesn't at all… — so it seems like using them instead of the "normal" mechanisms [events/callbacks/etc.] would always be an uphill battle and honestly doesn't seem worth it to me personally. But clearly there are many people who are using promises and would like a better solution for this library. I'll try take another look soon, and would appreciate others to chime in on the questions above, and maybe this can happen despite my lack of a personal "itch" for it. |
Wanted to write up some recent (design) progress I've made on this recently, especially a potential breakthrough I had the other day on this. First off, I think at least in first implementation the whole "how do you handle promise library" thing might be solvable by simply having a The bigger idea follows below. Background: I've not been excited about Promise support because one key "trick" Fermata uses is the presence of a callback function as signal of intent! That is to say However my realization is that the Promise API still requires the user to (eventually) provide a callback! So what if, after a user set The example would be something like this:
Or something like that. (To reiterate, I haven't voluntarily used Promises myself much so maybe there's a better example or I'm missing something that will prevent this from working.) The basic gist would be combining all three of whatever "normally" gets returned by the access mechanism, the Promise instance needed, and the usual request thing you can use to cancel it. Seems messy but might just work? |
Oh, I didn't call this out, but worth expounding on here: Another thing that made me squeamish about promises is that fermata now returns both data and metadata as two parameters (in addition to the error parameter which of course has its own mechanism with Promises). But promises can only be resolved with a single value, not two+. But I think the answer there is to just assume if Promises and the rest of ES6 take off, then you have destructuring anyway to clean it up a bit |
Any update of this? |
@ricxsar No, I haven't gotten much feedback lately and don't use Promises enough myself to judge how useful this would be. |
Update: we need Promise support for |
So even without #65 it might be possible to have every URL instance include a
|
Having (optional?) support for promises will allow for even more elegant usage with libaries like co. Since fermata is already using es6 features, it wouldn't be too farfetched to either look for a global promise object or expect clients to provide custom promise implementations before using the promise interface.
The text was updated successfully, but these errors were encountered: