November 6, 2014

Development Spotlight: Asynchronous Javascript and Promises via Chicago Javascript

The Chicago Javascript Meetup group hosted a....well, a meetup on two Javascript subjects.  This post focuses on the first, asynchronous Javascript. Users of Google Analytics certainly have heard the term "async" - asynchronous code was introduced in 2009. But what does “async" mean in general?  Here’s a brief run down:

JavaScript is a single-threaded language. The phrase "single-threaded" means that invoking a long-running process - a function - blocks execution of other processes until that process completes. Think of this as a moving up and down a ladder - you can’t really skip a rung to go down or up. Imagine that, and you have an idea of how JavaScript works with each function - one at a time.  It also draws imagery of what challenges can occur: single UI elements are unresponsive, animations pause, and no other code in the app can run.

The solution to these challenges is to minimize synchronous execution where possible. One way of deploying asynchronous JavaScript is to have a JavaScript function execute at a later time, as with event handlers, which are invoked after another call has raised an event. Callback functions are another kind of asynchronous processing, because they call back into the code that initiated the process.

Getting back to the presentation, Alex Castrounis, a JavaScript enthusiast who runs a Javascript site, Innoarchitech, asked the audience if they thought Javascript is asynchronous? They answer was no, it is not. He goes on to note how async and promises play hand in hand in improving JavaScript functionality.

Alex then goes through the Javascript engines that help set up the definition of promises. Promises are the result of an asynchronous results, allowing for executing multiple async calls without blocking event loops.  Alex explains in detail how they are used, when to use them, and their benefits in the slideshow below.

A note on JavaScript engines - Spidermonkey, chromenode.js and V8 are mentioned.  These are essential because their components - interpreter, heap (where objects and variable reside) , and call stack - play into the Message queue and Event Loop which are part of the promise function flow.

The Chicago Javascript group was formed in 2008.  You can see past talks and other information posted on the js.chi() site (http://javascriptchicago.com/).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram