-
Notifications
You must be signed in to change notification settings - Fork 50
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
[discussion] API for continuing the traversal #40
Comments
I'd like to see this handled the same way promises are. What I mean by that is that you can start with a common "promise" and then create multiple parallel promises from that. For example:
The point is that this code returns a promise for |
I only started using your library yesterday, so I don't have a full grasp of it yet :) I agree with @xogeny. I'd expect all new JS asynchronous APIs to be only Promise-based, and not support callback functions unless for legacy reasons. The one use case I had yesterday, was a HAL implementation of "streaming" paging. Here, every resource had a unique link to the next page of results. I'd like to be able to do this (ES6 syntax):
|
One thing I didn't really make clear here is that these chains need to be stateless. What I mean by that is that if I do something like this:
It's important that even thought |
I guess you can't have multiple parameters on a promise resolution. So perhaps the object passed to the continuation function should contain the response and the traversal at that point:
|
After initially proposing my idea/request to you, I started to build some kind of proof of concept myself, just to see if I could. Its nowhere near production ready, depends on the use of HAL, and only supports GET-Requests for now, but it shows how I thought this could work, which is a bit different from your approach, but not that far away from what xogeny and jinder are talking about here. Take a look, feel free to try it out with the maze API I did there - I hope it helps and inspires in some way. And if you have feedback yourself, shove it my way. |
First of all, thanks to all of you for your feedback <3 Alas, I'm not sure how this has turned into a discussion about promises versus callbacks 😲 Traverson has always been callback based and I don't see a compelling reaon to change that for the continuation feature. Reasons agains promises from my point of view:
@xogeny: The examples you show are totally possible with the callback based API. And for the statelessness/not-bleed-over-aspect,
That's okay but then your expectations are very different from mine 😉 @amadox: Since you mention the maze API, I also implemented a maze client as a proof of concept on top of the of |
Oh one more thing just came to my mind... |
It was worth a try :) The continue() feature seems to work for my use case, so I'm happy even if it isn't promise-based! |
This will be a bit awkward now :-( ... I asked for feedback, I got feedback and now it seems I don't even want to listen to the feedback because I'm not in inclined to introduce promises. Anyway, since nothing else except the promise/callback came up here and also since the discussion basically stopped after my 2 cents, I'm going forward with this. The I think I'll publish a version with this feature soon-ish. |
This has landed in 2.0.0. |
In various issues people have requested the possibility to continue the link traversal process when the initial traversal has finished (#7, #24, traverson-hal#4)
I started to work on continuing traversals in this branch.
The API currently looks like this:
The basic idea is that the callbacks given to the action methods (
get
,getResource
,post
, ...) are called with an additional, new parameter which represents the traversal process that has just been finished. This objecttraversal
currently offers only one method,continue()
which in turn returns a request builder instance which can be used just as the standard request builder. That is, it has the same configuration and action methods.Using
traversal.continue()
to initiate a new traversal ensures that the new traversal is started right where the first traversal stopped and it makes use of the last HTTP response of the first traversal to do so.This should even enable us to follow multiple relations from a resource because the request builder from
traversal.continue()
can be cloned withnewRequest()
as often as you lik to branch of multiple traversal processes from the current resource.My question: What do you think? Is this a reasonable API for this feature? Any suggestions?
The text was updated successfully, but these errors were encountered: