Boston Code Camp 22: Promises vs Callbacks

by Larry Spencer Saturday, November 22, 2014 12:11 AM
Tomorrow, I'll present a session on JavaScript Promises at Boston Code Camp 22. This post contains the materials from the first portion of my talk.

To contrast JavaScript promises with callbacks, I'll present a little game of my own devising. Written as a JavaScript program that has an ASP.NET Web API back end and AngularJS and JavaScript at the browser, the game simply consists of clicking as many dots as possible before time expires.

The dots are randomly generated through calls to the Web API Dot Service. When you click on a dot, the dot disappears and you get a point. What makes it interesting (only a little interesting, I'll admit) is that if you click on a region where more than one dot intersects, they all go away and you get points equal to the square of the number of intersecting dots. Make 3 dots vanish at once and get 9 points!

The Visual Studio 2013 solution is here as a ZIP file: (4.2MB). Different versions are in the folders

  • CallbacksWithoutErrorHandling
  • PromisesWithoutErrorHandling
  • CallbacksWithErrorHandling
  • PromisesWithErrorHandling
To switch between versions, edit index.html. To make error happen randomly so you can see how they're handled, un-comment the error-producing lines in Controllers\DotController.cs.

Have fun!

Tags: , ,

All | Talks

Add comment

About the Author

Larry Spencer

Larry Spencer develops software with the Microsoft .NET Framework for ScerIS, a document-management company in Sudbury, MA.