You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 19, 2018. It is now read-only.
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:
Deferred.define();
function makeRequest (method, params){
$.extend(params,{method: method, api_key: 123});
var url = $.param('http://api.example.com', params);
return next($.get(url))
}
Deferred.define(makeRequest, ["next","parallel"]);
makeRequest('getIdForUsername', {username:'dummy'}).
//user_id is from the response of 'getIfForUsername' request
makeRequest('getUserStatus', {user_id:user_id}).
next(function(userstatus){
//generate HTML for user status
});
My questions:
how can I "pass on" the user_id obtained in the first request to parameters of the second request?
How can I differentiate between parallell and sequential execution of the makeRequest- function?
How can I implement error handling proberly? (e.g. request1 gives an error response --> chain should stop and user should be notified of bad response)