Git Product home page Git Product logo

Comments (3)

edvakf avatar edvakf commented on July 30, 2024

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.

fbuchinger avatar fbuchinger commented on July 30, 2024

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.

edvakf avatar edvakf commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.