Using JSDOs to create mobile and web clients : JSDO overview : Asynchronous and synchronous execution
  

Asynchronous and synchronous execution

As described in the previous section, most JSDO methods operate locally on the client and execute synchronously in JavaScript. Synchronous execution means that the client waits for the method to complete execution and return its results before executing the next JavaScript statement or evaluating the next term of an expression. These results often include a method return value.
Other JSDO methods that access data, or otherwise operate on the server, execute asynchronously in JavaScript. These include the JSDO fill( ) and saveChanges( ) methods, which execute standard Data Object operations on the server, as well as the default execution of custom invocation methods (with or without use of the JSDO invoke( ) method), which execute custom Data Object Invoke operations on the server. Asynchronous execution means that immediately after the client calls the method, it continues to execute the next JavaScript statement or to evaluate the next term of an expression whether or not the asynchronous method completes and returns results. These results are returned to one or more callback functions that you define and that execute on the client according to certain conditions.
You can define a callback function (or callback) for an asynchronous method that can execute in one of the following ways:
*As a handler subscribed to a named event — Which executes after invoking the asynchronous method when a specified condition for firing the named event occurs. Thus, multiple callbacks can execute when the asynchronous method completes, depending on how many you subscribe to events that fire in response to invoking the method.
*As a callback registered using a method of a deferred Promise object — Which executes after invoking the asynchronous method when a specified condition occurs that is associated with the Promise object method that registered the callback. This Promise object, itself, is returned as the deferred value of the asynchronous method, allowing you to call one or more Promise object methods that register associated callbacks. Thus, multiple callbacks can execute when the asynchronous method completes, depending on how many of the conditions occur that are associated with the Promise object methods that you have called to register these callbacks.
Thus, the results of an asynchronous method execution only become available to the mobile app when either or both of the following occur, and in the following order:
1. An event associated with the asynchronous method fires, and a callback function that is subscribed to the event executes and receives the results as one or more input parameters.
2. A condition occurs that is associated with a deferred Promise object method that you have called to register a callback, and the callback executes and receives the results as one or more input parameters.
The callbacks that are available to return the results of asynchronous JSDO method calls depend on:
*The named events supported for a given JSDO method call. For more information, see Asynchronous execution using named events.
*Whether your environment supports Promises. For more information, see Asynchronous execution using Promises.
For more information on the differences between JSDO methods that execute asynchronously and JSDO methods that execute synchronously, see Comparing asynchronous and synchronous execution.
* Comparing asynchronous and synchronous execution
* Asynchronous execution using named events
* Asynchronous execution using Promises
* Managing JSDO event subscriptions
* Handling asynchronous and synchronous execution results
* General error handling for Data Object operations