-
Notifications
You must be signed in to change notification settings - Fork 48
How to access previous Ajax response in a chain of ajax requests #8
Comments
You can use Deferred.register
The easiest way is to bring the user_id to the outer scope.
I don't get your question. the "next" chain is always serial. Within the chain, you can use "parallel" if you want.
Put "error" in your chain, which handles any error occurs prior to that. If there is an error at "A" below, then B won't be executed but "C" and "D" will be executed.
You can do something like this. (I haven't run this, so excuse me if there is a mistake)
|
Thanks for your hints! I'm thinking about registering a second function that is responsible for storing the responses, so that I could write something like
I suppose saveResponse must return some function that takes care of storing the value: (?) Is there a way to "break" (stop executing) the chain programmatically? E.g. if the user enters a bogus username, I don't want the subsequent requests to be executed. |
At the moment there is no way for a registered function to receive an argument from the prior chain. Current implementation of Deferred.register is like this.
You see where there is "return this.next(function () {", this function should take the returned value of the chain. Unless you hack up your own "register", it's difficult. Instead, I would just pass an anonymous function to a "next"
If saveResponse returns a deferred object, then the chain continues after the saveResponse. (otherwise the chain continues without waiting for asynchronous saveResponse)
Maybe write "error" only at the end of the chain?
There is also Deferred.prototype.cancel, but I've never used it before. Looks like it's not suitable for this case. (not sure) |
Hi,
I'm writing a javascript client for a webservice that requires a lot of inter-dependent requests (i.e. you need the parts of the response from request1 as GET parameters in request 2). To avoid the extensive use of nested callbacks, I like to develop the client using jquery/jsdeferred's next statement. To make my code even shorter, I like to register my custom request/response handler as Deferred shorthand function. Here is what I have so far:
My questions:
Thanks for developing jsdeferred!
Franz
The text was updated successfully, but these errors were encountered: