Callback Hell: A guide to writing asynchronous JavaScript programs

<span class="keyword">var</span> photo <span class="keyword operator">=</span> <span class="function call">downloadPhoto</span>(<span class="string">'http://coolcats.com/cat.gif'</span>)
<span class="comment">// photo is 'undefined'!</span>

In this case the gif might take a very long time to download, and you don’t want your program to pause (aka ‘block’) while waiting for the download to finish.

Instead, you store the code that should run after the download is complete in a function. This is the callback! You give it to the downloadPhoto function and it will run your callback (e.g. ‘call you back later’) when the download is complete, and pass in the photo (or an error if something went wrong).

<span class="function call">downloadPhoto</span>(<span class="string">'http://coolcats.com/cat.gif'</span>, handlePhoto)

<span class="storage function">function</span> <span class="entity name function">handlePhoto </span>(error, photo) {
  <span class="keyword">if</span> (error) console.<span class="function call">error</span>(<span class="string">'Download error!'</span>, error)
  <span class="keyword">else</span> console.<span class="support method">log</span>(<span class="string">'Download finished'</span>, photo)
}

Github: run-parallel (javascript)

Run an array of functions in parallel

parallel Sauce Test Status

install

npm install run-parallel

usage

parallel(tasks, [callback])

Run the tasks array of functions in parallel, without waiting until the previous function has completed. If any of the functions pass an error to its callback, the main callback is immediately called with the value of the error. Once the tasks have completed, the results are passed to the final callback as an arr

Why did we build React?

When your data changes, the render method is called again. In order to perform updates as efficiently as possible, we diff the return value from the previous call to render with the new one, and generate a minimal set of changes to be applied to the DOM.

.. Because React has its own lightweight representation of the document, we can do some pretty cool things with it:

  • Facebook has dynamic charts that render to <canvas> instead of HTML.
  • Instagram is a “single page” web app built entirely with React and Backbone.Router. Designers regularly contribute React code with JSX.
  • We’ve built internal prototypes that run React apps in a web worker and use React to drive native iOS views via an Objective-C bridge.
  • You can run React on the server for SEO, performance, code sharing and overall flexibility.
  • Events behave in a consistent, standards-compliant way in all browsers (including IE8) and automatically use event delegation.