简体   繁体   中英

jQuery: Understanding .done() and .fail()

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM