Skip to main content
Skip table of contents

Client-Side JavaScript Mapping for Operations

Mapping for Operations

For each Slice operation defined on an interface, the generated proxy class provides a method with the same name. To invoke an operation, you call this method on the proxy.

For example, consider the following Slice definition:

SLICE
module VisitorCenter
{
    interface Greeter
    {
        string greet(string name);
    }
}

The generated proxy class (simplified) looks like this:

JS
class GreeterPrx extends Ice.ObjectPrx {
    constructor(communicator, proxyString) { ... }
  
    greet(name, context) { ... }

    // ...
}

And the TypeScript declaration:

TYPESCRIPT
export namespace VisitorCenter {
    export class GreeterPrx extends Ice.ObjectPrx {
        constructor(communicator: Ice.Communicator, proxyString: string);

        greet(name: string, context?: Map<string, string>): Ice.AsyncResult<string>;

        // ...
    }
}

Ice.AsyncResult extends the JavaScript Promise type. It adds functionality specific to Ice invocations.

Given a proxy to a Greeter object, a client can invoke greet as follows:

TYPESCRIPT
const greeter = new VisitorCenter.Greeter(
    communicator, 
    "greeter:tcp -h localhost -p 4061");

const greeting = await greeter.greet("Alice");  // Get name via RPC

Ice for JavaScript supports only asynchronous method invocation (AMI). The JavaScript runtime does not provide a blocking I/O model in order to keep the event loop responsive.

The arguments passed to the promise resolution depend on the operation signature:

  • If the operation has a single return value, the promise is fulfilled with that value.

  • If the operation has a return value and/or out parameters, the promise is fulfilled with an array: the return value (if any) followed by the out parameters.

Exception Handling

Any operation invocation may throw a local exception and, if the operation has an exception specification, may also throw user exceptions. Suppose we have the following simple interface:

SLICE
exception Tantrum
{
    string reason;
}

interface Child
{
    void askToCleanUp() throws Tantrum;
}

Slice exceptions are thrown as JavaScript exceptions, so you can simply enclose one or more operation invocations in a try-catch block:

TYPESCRIPT
const child = ...   // Get child proxy...

try {
    await child.askToCleanUp();
} catch (error: unknown) {
    if (error instanceof Tantrum) {
        console.log("The child says:", error.reason);
    } else {
        throw error;
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.