Wednesday, January 8, 2014

chaining .then onto potential points of failure in dojo code

updateFoo: function(foo) {
   foo.somethingFluffy = "bunnies";
   dataFunctions.addFooHistoryRecord(foo).save();
   return foo.save();
}

 
 

...isn't so good. You could save a history record, but not a foo if things go South at the right point in time. A better way:

updateFoo: function(foo) {
   foo.somethingFluffy = "bunnies";
   return foo.save()
      .then(lang.hitch(this, function(){
         dataFunctions.addFooHistoryRecord(foo).save();
      }), lang.hitch(this, function(err) {
         console.log('Yikes!');
      }));
}

No comments:

Post a Comment