Asynchronous APIs in JavaScript

Given JavaScript's lack of support for threads, Ice for JavaScript uses asynchronous semantics in every situation that has the potential to block, including both local and non-local invocations. Synchronous proxy invocations are not supported.

Here is an example of a simple proxy invocation:

JavaScript
var proxy = HelloPrx.uncheckedCast(...);
var asyncResult = proxy.sayHello();
asyncResult.then(
    function(ar) {
        // handle success...
    },
    function(ex, ar) {
        // handle failure...
    }
);

The API design is similar to that of other asynchronous Ice language mappings in that the return value of a proxy invocation is an instance of Ice.AsyncResult, through which the program configures callbacks to handle the eventual success or failure of the invocation. The JavaScript implementation of Ice.AsyncResult also provides promise functionality.

Certain operations of local Ice run-time objects can also block, either because their implementations make remote invocations, or because their purpose is to block until some condition is satisfied:

  • Communicator::destroy
  • Communicator::waitForShutdown
  • Communicator::createObjectAdapterWithRouter
  • Communicator::flushBatchRequests
  • Connection::close
  • Connection::flushBatchRequests
  • ObjectPrx::ice_getConnection

These operations use the same asynchronous API as for proxy invocations. The example below shows how to call ice_getConnection on a proxy:

JavaScript
var communicator = ...;
var proxy = communicator.stringToProxy("...");
proxy.ice_getConnection().then(
    function(connection, asyncResult) {
        // got connection...
    },
    function(ex, asyncResult) {
        // connection failed...
    }
);