0

I'm jumping into a project authored by another developer, and am trying to make my way around the codebase.

Here's the code I'm having trouble with:

var ret;
ret = new $.Deferred();

new Parse.Query(Model).include('companyDetails').matchesQuery('companyDetails', 
new Parse.Query(UserModel).equalTo('objectId', 'seppWfGi20')).first().done((function(_this) {
  return function(person) {
    if (person) {
      _this.data.person = person;
      return ret.resolve(_this.data.person);
    } else {
      return ret.reject('person');
    }
  };
})(this)).fail((function(_this) {
  return function(error) {
    return ret.reject('person');
  };
})(this));

ret;

I can't work out what the .done() and .fail() methods are doing. Also, what's the .first() there for?

Can any jQuery ninjas help explain what this code is doing, step-by-step?

Any help is appreciated. Thanks in advance!

realph
  • 4,481
  • 13
  • 49
  • 104
  • 1
    https://api.jquery.com/deferred.done/ https://api.jquery.com/deferred.fail/ – Kevin Boucher Jul 01 '15 at 20:44
  • Uh, given that parse does return promises already, this is a form of the [deferred antipattern](http://stackoverflow.com/q/23803743/1048572) – Bergi Jul 01 '15 at 23:42

1 Answers1

0

These are a form of promises. In this instance, it allows explicit functions to be called when success/failure happens. It's the equivalent of doing…

Parse.query({query here}, {
    success: function(response){}, // this is the done function
    error: function(response){} // this is the fail function
})

Finally, the .first() is Parse specific telling it to get the first result in the query set. I'd say you'd be better off untangling that mess of IIFE statements.

Chad
  • 200
  • 4
  • I'm playing around with this, and trying to call `.last()` to get the last result, but it isn't working is giving me an error of .last() is not a function. Any idea what's wrong here? – realph Jul 01 '15 at 21:36