393
Chapter 9 Asynchronous operations
CHAPTER 9 Asynchronous operations
H
ave you ever run an application and had the screen freeze when you clicked a button?
You waited until its operation completed, and then the application started working
again. Have you seen this behavior on webpages? You know that the problem is due to
long-running operations, but shouldn’t there be a better way to write this code?
The answer to these questions is to implement long-running operations as asynchronous
operations because these operations are currently synchronous operations. Synchronous
code has one sequential execution path, and when the long-running code is executing,
nothing else can be accomplished. What’s needed is another execution path so that the
long-running code can execute on one execution path while the user interface is responsive
on another execution path; this is what asynchronous operations do.
This chapter explores asynchronous operations, using jQuery and web workers.
Lessons in this chapter:
■■Lesson 1: Asynchronous operations using jQuery and WinJS 393
■■Lesson 2: Working with web workers 404
Before you begin
To complete this book, you must have some understanding of web development. This
chapter requires the hardware and software listed in the “System requirements” section in
the book’s Introduction.
Lesson 1: Asynchronous operations using jQuery and WinJS
An asynchronous operation is executed on a thread that is different from the main thread,
a thread being a separate execution path. The previous chapter provided a brief introduc-
tion to asynchronous programming when executing AJAX calls to the server. In addition to
jQuery’s support for using AJAX calls, you might want to perform local asynchronous calls
for operations that are time consuming. For example, if you are performing animations by
using jQuery, you’ll find that jQuery animations are performed asynchronously.
Key
Te rms