Comments (3)
To make my code even shorter, I like to register my custom request/response handler as Deferred shorthand function.
You can use Deferred.register
- how can I "pass on" the user_id obtained in the first request to parameters of the second request?
The easiest way is to bring the user_id to the outer scope.
- How can I differentiate between parallell and sequential execution of the makeRequest- function?
I don't get your question. the "next" chain is always serial. Within the chain, you can use "parallel" if you want.
- 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)
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.
next(A).next(B).error(C).next(D);
You can do something like this. (I haven't run this, so excuse me if there is a mistake)
function makeRequest(method, params) {
$.extend(params,{method: method, api_key: 123});
var url = $.param('http://api.example.com', params);
return $.get(url);
}
Deferred.register('makeRequest', makeRequest);
var user_id;
makeRequest('getIdForUsername', {username:'dummy'})
.next(function(s){
user_id = s;
})
.error(function(err){
// error in request 1
})
.makeRequest('getUserStatus', {user_id:user_id})
.next(function(userstatus){
//generate HTML for user status
})
.error(function(err){
// error in request 2
});
from jsdeferred.
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
Deferred.register('makeRequest', makeRequest);
Deferred.register('saveResponse', saveResponse);
makeRequest('getIdForUsername', {username:'dummy'})
.saveResponse('user_id')
I suppose saveResponse must return some function that takes care of storing the value: (?)
var savedKeys = {};
function saveResponse(key){
var key = key;
return function (s){
savedKeys[key] = s.key
}
}
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.
from jsdeferred.
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.
Deferred.register = function (name, fun) {
this.prototype[name] = function () {
var a = arguments;
return this.next(function () {
return fun.apply(this, a);
});
};
};
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"
makeRequest('getIdForUsername', {username:'dummy'})
.next(function(user_id) {
return saveResponse(user_id)
})
.next(...)
If saveResponse returns a deferred object, then the chain continues after the saveResponse. (otherwise the chain continues without waiting for asynchronous saveResponse)
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.
Maybe write "error" only at the end of the chain?
next(function() {
throw 'INVALID_USERNAME';
})
.next(function(){}) // not executed
.next(function(){}) // not executed
.next(function(){}) // not executed
.next(function(){}) // not executed
.next(function(){}) // not executed
.next(function(){}) // not executed
.next(function(){}) // not executed
.next(function(){}) // not executed
.error(function(err) {
if (err === 'INVALID_USERNAME')
// alert
});
There is also Deferred.prototype.cancel, but I've never used it before. Looks like it's not suitable for this case. (not sure)
from jsdeferred.
Related Issues (16)
- IEでSSL通信時に警告が出ることがある HOT 5
- Use XMLHttpRequest instead of GM_xmlhttpRequest in Google Chrome HOT 1
- export は Safari で予約語なのでパース失敗します HOT 2
- Wrong process sequence with using Deferred.connect HOT 1
- Maybe use mime type for data URI in Deferred.next_faster_way_Image HOT 2
- Deferred.define()してないとエラーになる HOT 1
- Add throtteling to parallel function execution HOT 1
- bug in parallel method? HOT 2
- Does not pass JSLint HOT 1
- jQuery binding should treat jQuery.Deferred as our Deferred. HOT 1
- Append more typical sample codes to cookbook
- Append future function
- Image corrupt or truncated HOT 1
- chainの例が最後まで実行されない
- Bug in parallel method
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsdeferred.