0

I'm not really sure what is the difference between next:

$(document).ready(function() {
  function resolveAtOnce() {
    var d = $.Deferred();

    // return d.resolve("Ok 1").promise(); // works
    return d.resolve("Ok 2"); // works also
    //d.resolve("Ok 3");

    return d.promise();
  }

  resolveAtOnce().done(function(response) {
    console.log("Resolved with response: '" + response + "'");
  }).fail(function(error) {
    console.log("Resolved with response: '" + error + "'");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Is there any difference between all 3 approaches? Everything seems to work. Running example: https://jsfiddle.net/u819mv7e/.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
broadband
  • 3,266
  • 6
  • 43
  • 73
  • 1
    Returning the promise in #1 from a resolved deferred object doesn't make much sense - you already have a reference to it. In a real world example you also wouldn't immediately `return` the resolved promise, so #2 is a little odd. #3 is the most common usage as the `resolve()` will be called after whatever async block of code you're waiting for has completed. – Rory McCrossan Jun 07 '18 at 12:44
  • Yes, you can return a promise from a function. All your three variants do exactly that. Soo... what's your question again? – Tomalak Jun 07 '18 at 12:59
  • @Tomalak According to this post it is a difference: https://stackoverflow.com/questions/7641964/what-is-the-difference-between-resolve-and-promise. – broadband Jun 07 '18 at 13:14
  • @RoryMcCrossan what if I have next function: `function calculate(item) { var d = $.Deferred(); if(item.alreadyCalculated()) return d.resolve("ok"); else async_call_calculate ....` In this case I have a need to immediately return resolved promise. – broadband Jun 07 '18 at 13:15
  • @RoryMcCrossan perhaps then #3 is the most appropriate. – broadband Jun 07 '18 at 13:18
  • In that case you would call `d.resolve()` then `return d;` separately as technically logic blocks should have as few exit points as possible. – Rory McCrossan Jun 07 '18 at 13:18

1 Answers1

1

Is there a difference between these?

return d.resolve("Ok 1").promise(); // works

This returns a resolved Promise object. At the interface level, promises are defined as objects that at least support a .then() method. (That's why those kinds of objects are also called thenable.)

In jQuery and for historic reasons—jQuery implemented promises before there was a stable standard—they also support .done(), and .fail(), and a few others.

return d.resolve("Ok 2"); // works also

This returns a resolved Deferred object. Deferreds are a superset of promises, which means they are thenable and support all the same methods jQuery promises support, and can be treated as if they were promise objects.

The difference between Deferred objects and Promise objects is that promises have a fixed state which cannot be changed after resolution, but the state of Deferreds can actually be changed.

d.resolve("Ok 3");
return d.promise();

This is the same as example #1.

So, what should you use?

When working with jQuery deferreds, always return .promise(), because they comply with the requirement to have an unchangeable state.

Tomalak
  • 332,285
  • 67
  • 532
  • 628