Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Boom! Promises/A+ Was Born

26,832 views

Published on

(Presented at JSConf US 2013. Be sure to check out the speaker notes!)

Frustration, a rant, a test suite, a gist. Then, community awesomeness. Boom! Promises/A+ was born.

Promise are an old idea for abstracting asynchronous code, but have only recently made their way into JavaScript. We'll look at the power they provide via two striking examples that go beyond the usual "escape from callback hell" snippets. First we'll show how, with ES6 generators, they can act as shallow coroutines to give us back code just as simple as its synchronous counterpart. Then we'll look at how they can be used as proxies for remote objects, across <iframe>, worker, or web socket boundaries.

However, the most interesting aspect of Promises/A+ is not just the code it enables, but how we worked to create it. We didn't join a standards body, but instead formed a GitHub organization. We had no mailing list, only an issue tracker. We submitted pull requests, made revisions, debated versions tags, etc.—all in the open, on GitHub. And, we succeeded! Promises/A+ is widely used and implemented today, with its extensible core forming the starting point of any discussions about promises. Indeed, this community-produced open standard has recently been informing the incorporation of promises into ECMAScript and the DOM. I'd like to share the story of how this happened, the lessons we learned along the way, and speculate on the role such ad-hoc, community-driven, and completely open specifications have for the future of the web.

Published in: Technology
  • Be the first to comment

Boom! Promises/A+ Was Born

  1. 1. PROMISES/A+WAS BORNBOOM!
  2. 2. HI, I’M @DOMENIC
  3. 3. The JavaScript community’sgreatest strengthis that we turn tiny primitives intopowerful patterns.
  4. 4. But it is alsoour greatest weakness.
  5. 5. The Promises/A+ story is a story whereinthe community overcame this weakness.
  6. 6. Let’s talk about async.
  7. 7. CONTINUATION PASSING STYLEvar fileName = "data.txt";fs.readFile(fileName, function (err, data) {// ...});
  8. 8. CPS is easy.But it is not simple.
  9. 9. CPS traps you in the Turing Tarpit.return and throw, where did you go!
  10. 10. ENTER PROMISES Joule and E’s promises Java’s java.util.concurrent.Future Python’s Twisted deferreds and PEP-3148 futures F#’s Async<T> .NET’s Task<T> C++11’s std::future Dart’s Future<T> JavaScript’s Promises/A+
  11. 11. The point of promises is simple:to give you back async versions ofreturn and throw.
  12. 12. function* someNumbers() {console.log("A");yield 1;console.log("B");yield 2;console.log("C");yield 3;console.log("D");}var iterator = someNumbers();console.log(iterator.next()); // "A", { value: 1, done: false }console.log(iterator.next()); // "B", { value: 2, done: false }console.log(iterator.next()); // "C", { value: 3, done: false }console.log(iterator.next()); // "D", { value: undefined, done: true }http://jsbin.com/eqamor/1/edit
  13. 13. Q.async(function* () {$("#loading").text("Loading...").fadeIn();try {var repoEvents = yield getRepoEvents("kriskowal", "q");updateUI(repoEvents);$("#loading").text("Loaded!");} catch (e) {$("#loading").text("Error loading data: " + e.message);} finally {yield Q.delay(5000);$("#loading").fadeOut();}})();http://jsbin.com/igimow/1/edit
  14. 14. As a bonus, we get time travel:promises are objects representingobjects from a different time.expect(promise).to.eventually.deep.equal(["zomg", "jsconf!"]);
  15. 15. // ES5userPromise.get("repos").get(0).get("commitHistory").invoke("toHTML").then(displayInUI).done();// ES6const html = yield userPromise.repos[0].commitHistory.toHTML();displayInUI(html);
  16. 16. // ES5userPromise.get("repos").get(0).get("commitHistory").invoke("toHTML").then(displayInUI).done();// ES6const html = yield userPromise.repos[0].commitHistory.toHTML();displayInUI(html);
  17. 17. var Connection = require("q-connection");var remote = Connection(port);// a promise for the remote user object!var userPromise = remote.invoke("getUser", "domenic"); Web Socket Web Worker Message Porthttps://github.com/kriskowal/q-connection/
  18. 18. PROMISES IN JAVASCRIPTThe path to Promises/A+ has beenlong and treacherous.
  19. 19. It all started with CommonJS Promises/Ahttps://groups.google.com/d/msg/commonjs/6T9z75fohDk/U_0Gl9fPJxsJ
  20. 20. Enter $.Deferredhttp://bugs.jquery.com/ticket/11010
  21. 21. Not Again!!https://github.com/emberjs/ember.js/pull/1406Horrible lies!
  22. 22. https://gist.github.com/domenic/3889970
  23. 23. https://github.com/domenic/promise-tests
  24. 24. https://gist.github.com/briancavalier/eb5fc157825a170c9957
  25. 25. http://promisesaplus.com
  26. 26. ok, so what?
  27. 27. https://github.com/promises-aplus/promises-spec/blob/master/implementations.md
  28. 28. http://dom.spec.whatwg.org/#futures
  29. 29. And if you don’t have an API you mightas well set up some Futures. Just in case.
  30. 30. http://wiki.ecmascript.org/lib/exe/fetch.php?media=strawman:roadmap.pdf
  31. 31. … what just happened?
  32. 32. OPEN SPECIFICATION DEVELOPMENTWhat made the Promises/A+ effort work so well? The cause The people The code The contract The setting
  33. 33. the cause:bringing sane asynchronicity to JS
  34. 34. the people:a strong and cooperative community
  35. 35. the code:existing convergent and widely-loved solutions
  36. 36. the contract:a small core for interoperation via standardization
  37. 37. the setting:GitHub
  38. 38. An open standard for sound, interoperable JavaScriptpromises—by implementers, for implementers.
  39. 39. With these ingredients, you can extend the web forward.http://yehudakatz.com/2013/05/21/extend-the-web-forward/
  40. 40. THANKS!promisesaplus.com@promisesaplus

×