0

I've been programming in JavaScript for a couple years and never heard the term Promise until recently. I've read multiple articles on the web about it and still don't understand what a Promise is. I don't see any rigorous definition. Every example I've seen is of a problem I've already known how to solve. For example,

get('story.json').then(function(response) {
  console.log("Success!", response);
}, function(error) {
  console.error("Failed!", error);
});

from https://davidwalsh.name/promises I would've already known how to do like

$.ajax({
   url : 'story.json',
   method : 'GET',
   success : function(response) { console.log("Success!", response); },
   error : function(error) { console.error("Failed!", error); }
});

So was I using the concept of a Promise without knowing the term? Or where's the big party that I'm missing out on?

user6048670
  • 2,861
  • 4
  • 16
  • 20
  • Your second example is the jQuery implementation of a promise. – ephbaum May 22 '16 at 02:02
  • 2
    @fskirschbaum Not at all... – plalx May 22 '16 at 02:02
  • @plalx Sure it is. Though it depends on the jQuery version you're working with, of course, but it's a deferred and gives the same 'result' as the asker is talking about, but in essence, yes, it's the same concept. Also, and I have to do this. jQuery !== JavaScript – ephbaum May 22 '16 at 02:05
  • [__Promise__](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) – Rayon May 22 '16 at 02:14
  • The syntax here is certainly not current, though. Now you'd really want to use the chain with `.done()` as opposed to passing the settings object. The answer to your question is a bit more complicated, but the concept of a deferred with the XHR Object using either the `success:` or the `.done()` callbacks function similarly but no, are not actually Promises. [jQuery Deferred](http://api.jquery.com/category/deferred-object/), [jQuery .promise](http://api.jquery.com/promise/), [JavaScript Promise Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) – ephbaum May 22 '16 at 02:14
  • Possible duplicate of [What is a promise object?](http://stackoverflow.com/questions/26672317/what-is-a-promise-object) – Patrick Evans May 22 '16 at 02:18
  • See https://www.promisejs.org#definition , http://stackoverflow.com/questions/29268569/what-is-the-correct-terminology-for-javascript-promises – guest271314 May 22 '16 at 02:21
  • Possible duplicate of http://stackoverflow.com/questions/22539815/arent-promises-just-callbacks – Benjamin Gruenbaum May 22 '16 at 14:28

1 Answers1

2

The addition is being able to chain things. If you need to make three calls which all have to happen one after the other, you can chain your promises together rather than:

$.ajax({
  success () {
    $.ajax({
      success () {
        $.ajax({
          success () { /* do something with your sets of results */ }
        });
      }
    });
  }
});

instead, you can do something like:

fetch(url1).then(toJSON)
  .then(result1 => fetch(url2).then(toJSON))
  .then(result2 => fetch(url3).then(toJSON));

These two examples aren't doing exactly the same thing (you'd need to use each result, or pass it out, to pass it on), but basically promises wrap your process and return you an object (with a .then method) which allows you to add callbacks (and keep adding them).

Norguard
  • 26,167
  • 5
  • 41
  • 49