Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What really made it all click for me was <a href="http://www.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript" rel="nofollow noreferrer">this presentation</a> by Domenic Denicola.</p> <p>In a <a href="https://gist.github.com/3889970" rel="nofollow noreferrer">github gist</a>, he gave the description I like most, it's very concise:</p> <blockquote> <p><em>The point of promises is to give us back functional composition and error bubbling in the async world.</em></p> </blockquote> <p>In other word, promises are a way that lets us write <em>asynchronous</em> code that is almost as easy to write as if it was <em>synchronous</em>.</p> <p>Consider this example, with promises:</p> <pre><code>getTweetsFor("domenic") // promise-returning async function .then(function (tweets) { var shortUrls = parseTweetsForUrls(tweets); var mostRecentShortUrl = shortUrls[0]; return expandUrlUsingTwitterApi(mostRecentShortUrl); // promise-returning async function }) .then(doHttpRequest) // promise-returning async function .then( function (responseBody) { console.log("Most recent link text:", responseBody); }, function (error) { console.error("Error with the twitterverse:", error); } ); </code></pre> <p>It works as if you were writing this synchronous code:</p> <pre><code>try { var tweets = getTweetsFor("domenic"); // blocking var shortUrls = parseTweetsForUrls(tweets); var mostRecentShortUrl = shortUrls[0]; var responseBody = doHttpRequest(expandUrlUsingTwitterApi(mostRecentShortUrl)); // blocking x 2 console.log("Most recent link text:", responseBody); } catch (error) { console.error("Error with the twitterverse: ", error); } </code></pre> <p>(If this still sounds complicated, watch that presentation!)</p> <p>Regarding Deferred, it's a way to <code>.resolve()</code> or <code>.reject()</code> promises. In the <a href="http://wiki.commonjs.org/wiki/Promises/B" rel="nofollow noreferrer">Promises/B</a> spec, it is called <code>.defer()</code>. In jQuery, it's <code>$.Deferred()</code>.</p> <p>Please note that, as far as I know, the Promise implementation in jQuery is broken (see that gist), at least as of jQuery 1.8.2.<br> It supposedly implements <a href="http://wiki.commonjs.org/wiki/Promises/A" rel="nofollow noreferrer">Promises/A thenables</a>, but you don't get the correct error handling you should, in the sense that the whole "async try/catch" functionality won't work. Which is a pity, because having a "try/catch" with async code is utterly cool.</p> <p>If you are going to use Promises (you should try them out with your own code!), use <a href="https://github.com/kriskowal/q" rel="nofollow noreferrer">Kris Kowal's Q</a>. The jQuery version is just some callback aggregator for writing cleaner jQuery code, but misses the point.</p> <p>Regarding Future, I have no idea, I haven't seen that in any API.</p> <p><strong>Edit:</strong> <a href="http://www.youtube.com/watch?v=hf1T_AONQJU" rel="nofollow noreferrer">Domenic Denicola's youtube talk on Promises</a> from <a href="https://stackoverflow.com/users/286588/farm">@Farm</a>'s comment below.</p> <p>A quote from Michael Jackson (yes, <em>Michael Jackson</em>) from the video:</p> <blockquote> <p>I want you to burn this phrase in your mind: <em>A promise is an <strong>asynchronous value</em></strong>.</p> </blockquote> <p>This is an excellent description: a promise is like a variable from the future - a first-class reference to something that, at some point, will exist (or happen).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload