«Актуальные вопросы в сфере социально-экономических, технических и естественных наук и информационных технологий» (3-4 апреля 2014г.)

Khizha A. L., Shvayka A. A., Rusakova A.V.

Oles Honchar Dnipropetrovsk National University


In JavaScript, functions are called synchronously or asynchronously. When asynchronous method is invoked, the execution of parent function continues. After it is done, asynchronous invocation fires the callback function. On the other hand, synchronous call detains parent method until the result value is returned. In the programming practice, the need of replacing synchronous invocations with asynchronous ones encounters occasionally. For example, a conditional construct with multiple logical disjunction:


Fig. 1

By all means, asking user something is the last resort, that is why dialog call is in the end. Unfortunately, there is no synchronous dialog method in BlackBerry 10 WebWorks API (it was deprecated after OS 7). Thus, it is impossible to implement example above. To get the dialog result synchronously, it is necessary to develop a function that behaves like await keyword in C#, what is complicated, because client-side JavaScript (like website or BlackBerry WebWorks application) is a single-threaded programming language (except Worker object).

In JavaScript, some objects like FileReader and XMLHttpRequest have synchronous methods. File API is still in development and working with filesystem is heavy on resources. That is why it will be better to use XMLHttpRequest object instead, which is able to send requests synchronously. This can be achieved by setting third argument of open method to false.

It is well known that http:// and https:// protocols can be used to make requests. Far less known that the usage of file:/// and local:/// is also possible within URL string of a request. That is why it is possible to send requests to any existing file in a loop while waiting for asynchronous invocation to call back:


Figure 2.

Unfortunately, this operation will result in high CPU and storage load. That will certainly heat up the device and drain a large amount of battery power. However, there is another way:


Fig. 3

Usually requests are asynchronous and their methods are not able to generate exceptions (unless navigator is experiencing some connection issues). On the other hand, synchronous requests throw exceptions if the destination is unreachable. That is exactly what is happening in example above. The loop does not overload hardware that much because the filesystem is idle. Also, it can be interrupted by WebWorks API (but not by any asynchronous function in JavaScript) methods callbacks. This is what makes synchronization of asynchronous methods possible.

The list of references:

1. Flanagan David. JavaScript: The Definitive Guide / David Flanagan // O’Reilly Media. – 2011. – April.

2. Ребров А. FileSystem API & File API / А. Ребров // Habrahabr. – 2011, January.

3. Bidelman Eric. New Tricks in XMLHttpRequest2 / Eric Bidelman // HTML5 Rocks. – 2011. – May.