Using JSDOs to create mobile and web clients : Accessing custom Invoke operations : Asynchronous vs. synchronous method execution
  

Asynchronous vs. synchronous method execution

You can execute invocation methods either asynchronously (the default behavior) or synchronously, depending on how you call them. A JSDO provides two basic mechanisms for calling an invocation method:
*Calling the method by name — Invoking the method on the JSDO instance like any other JSDO method. This allows you to call the method asynchronously or synchronously according to a parameter that you pass, with all asynchronous results handled by event callbacks.
*Passing the method as a parameter to the JSDO invoke( ) method — Invoking the invoke( ) method on the JSDO instance, passing as parameters the name of the method and an object containing properties whose names match the names of the input parameters defined for the server method that implements the Invoke operation. This allows you to call the invocation method asynchronously only, with results handled either (or both) using event callbacks or callbacks that you register using a Promise object returned as the value of the invoke( ) method.
If you call the invocation method by name, along with an optional object parameter containing properties that match any input parameters defined for the implementing server method, you can pass a second optional boolean parameter that specifies the execution mode. A value of true (the default) for this second parameter specifies asynchronous execution; for synchronous execution, set the parameter to false.
To process the results of an asynchronous method call, you can subscribe an event-handler callback function to the afterInvoke event, or if you use the invoke( ) method, register callbacks on the returned Promise object by calling any of the available Promise methods (see Asynchronous execution using Promises). All Invoke operation callbacks have the same signature, which includes a returned request object from which you can return results by accessing its response property.
For a synchronous call, you do not use callbacks, but access results through the response property of the request object that is returned as the value of the invocation method.
Note: The recommended execution mode is asynchronous, because every invocation method call sends a request across the network. There are some cases where you might want to execute the method synchronously, for example, if you want to use the return value in an expression and the request response time is likely to be relatively short. However, please note that some web browsers are deprecating synchronous network requests in some or all situations.