Podcast: Play in new window | Download
Subscribe: Apple Podcasts | Spotify | TuneIn | RSS
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.eu/speakers/forbes-lindesay-promises-and-generators-control-flow-utopia.html
- http://javascriptplayground.com/blog/2015/02/promises/
- http://www.mattgreer.org/articles/promises-in-wicked-detail/
- http://www.toptal.com/javascript/javascript-promises
- http://taoofcode.net/promise-anti-patterns/
- https://gist.github.com/domenic/3889970
Tips for this Episode
Allen’s Tip:
Copy and Paste XML or JSON into Visual Studio and have it auto-generate classes to represent the data
1. Copy the XML or JSON
2. In Visual Studio 2012 & up, click the Edit menu, then Paste Special and choose XML as Classes or JSON as Classes
Michael’s Tip:
git grep -e ‘TEXT-TO-SEARCH-FOR’ $(git rev-list –all –max-count=500)
-e is necessary for strings that begin with a dash and should be used for scripts where input is passed to git grep.
Quoting the pattern string is optional, but a good practice.
–max-count is necessary if you get errors like “argument list too long” or “bad file number”. Both mean that the rev-list is too large.
Joe’s Tip:
Templates / Themes built for easy integration with ASP.NET websites. Buy a design that’s ready to open up in Visual Studio.
http://www.wiwet.com
Tip from Matthew Watkins:
Ability to assign macros to any keys you designate in Windows.
http://ahkscript.org/