CodingBlocks
Episode 31 – Javascript Promises and Beyond
In this episode we dive into Javascript Promises. If you're used to the olden way of doing an async call with a callback, you'll definitely want to give this episode a listen as you could improve your mental sanity 10 fold by learning how to use promises in your application! Survey - What is Your Favorite Language? News ConnectJS is coming soon. If you're in the Atlanta area, definitely check it out. There's a lot of big names involved with this conference. http://connect-js.com/ Attended a couple of excellent Meetups MeteorJS and PixiJS in Gaming - here's a YouTube video of the Meetup: https://youtu.be/UV5NkatJQRQ Introduction to Flux - here's the overview on Facebook https://facebook.github.io/react/blog/2014/05/06/flux.html Learning Aurelia JS with Julie Lerman - excellent interview from the .NET Rocks podcast https://www.dotnetrocks.com/default.aspx?showNum=1180 We're on SoundCloud!!!! https://soundcloud.com/codingblocks Answered an E-mail from Steven Lopes regarding global variables ViewState is an example of the Memento Pattern - thanks to Wesley Bakker https://mscblogs.blob.core.windows.net/media/wesleybakker/TS/index.html Getting some love from people on Reddit and StackOverflow! Thanks to everyone who shares us everywhere. Survey Results from the last episode... Apparently a majority of people agreed with Allen and Joe - use a framework to kickstart what you're trying to learn and pick up the other pieces as you go. Javascript Promises Listing of popular JS Libraries for Promises Bluebird - one of the fastest around - MIT https://github.com/petkaantonov/bluebird ES6 Promise Polyfill - MIT - utilizes subset of RSVP that is per ECMA 6 specs https://github.com/jakearchibald/es6-promise jQuery - MIT - non-standard promise type implementation http://jquery.com/ kew - Apache 2.0 - optmized version of the Q library https://github.com/Obvious/kew lie - MIT - looks like it's ES6 compliant and can be used in node https://github.com/calvinmetcalf/lie Q - MIT - Very popular - lite version in AngularJS https://github.com/kriskowal/q RSVP - MIT - works in the browser and node and is ES6 compliant plus some additional goodies https://github.com/tildeio/rsvp.js What are they and why do they matter? Allow for an easier way to handle asynchronous requests with a much cleaner approach to error handling. Why is it so hot now? Probably due to the explosion of so many Javascript libraries that all take advantage of asynchronous requests Core Concepts Async requests are "thenable" - meaning you do asyncRequest.then(funcSuccess, funcError).then(funcSuccess, funcError) - you can just keep chaining asynchronous requests together. Each promise has a few states. Pending, Fulfilled, Rejected Each request has the ability to catch an error using the funcError shown above - if included, the funcSuccess will be skipped and the funcError will be executed if there was a problem. Additionally, you can use a catch rathe than the option funcError - asyncRequest.then(funcSuccess).then(funcSuccess).catch(funcError) Once executed, they always resolve to the same value - in other words, if you call the same promise twice, you will get back the same result and the async rquest only gets called ONCE! This is great for performance but could really mess you up if you weren't aware of this. The requests can't be canceled and there is no progress indicator. Promises in Other Languages? C# has tasks Java has futures/promises sort of Scala and C++, Python, etc "Tasks" are built in, "Promises" are more of a convention Resources We Like https://promisesaplus.com/ http://complexitymaze.com/2014/03/03/javascript-promises-a-comparison-of-libraries/ http://www.html5rocks.com/en/tutorials/es6/promises/ https://www.promisejs.org/ http://pag.forbeslindesay.co.uk/#/ http://2013.jsconf.