Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
What is the difference in jQuery between returning deferred and deferred.promise()?

I'm working in an ES5 environment using jQuery. I have some code as follows:

var saveGame = function (gameState, saveStars) {
  var deferred = $.Deferred();
  var starsSaved = saveStars ? false : true;

  model.game().saved(starsSaved);
  model.driveAccessInProgress(true);

  GW.manifest.saveGame(gameState).then(function () {
    model.driveAccessInProgress(false);
    deferred.resolve();
  });
  return deferred; // or deferred.promise()?
};

saveGame().then(/* more stuff */);

In this instance, whether I return deferred.promise() or just deferred the .then works as expected. Given this, I guess I'm a little unclear on what a promise is, and was wondering what the difference is between these two returns and when might it matter?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You want to return deferred.promise() so that the calling code can't call resolve or reject or other Deferred-specific methods. That's not something the caller should have access to. It should only be able to consume the promise, not affect its state. You can read the documentation as well .

about 4 years ago · Juan Pablo Isaza Report

0

You would return deferred.promise().

But it looks like saveGame(gameState) already returns a Promise so you shouldn't use $.Deferred here at all.

var saveGame = function (gameState, saveStars) {
  var starsSaved = saveStars ? false : true;

  model.game().saved(starsSaved);
  model.driveAccessInProgress(true);

  return GW.manifest.saveGame(gameState).then(function () {
    model.driveAccessInProgress(false);
  });
};

saveGame().then(/* more stuff */);

Having already a Promise and creating a new one using $.Deferred is an anti-pattern. the reason for this is that you might easily forget certain cases and your code might get stuck on such a point.

What if e.g. GW.manifest.saveGame(gameState) fails? You don't consider that case so you would need to add a .catch:

  GW.manifest.saveGame(gameState).then(function () {
    model.driveAccessInProgress(false);
    deferred.resolve();
  })
  .catch(function(err) {
    deferred.resolve(err);
  })

And there might be other cases as well.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!