From 3e9df018a8fafaf0bcf220992a481f740a8409fa Mon Sep 17 00:00:00 2001 From: Eryu Xia Date: Wed, 30 Aug 2023 17:46:47 -0700 Subject: [PATCH] Document on how to use Promises --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 3ebc3e25..57bff882 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,8 @@ code. * `echo_pb.d.ts` - Generated by `--grpc-web_out`, contains TypeScript definitions for `echo_pb.js`. +### Using Callbacks + ```ts import * as grpcWeb from 'grpc-web'; import {EchoServiceClient} from './EchoServiceClientPb'; @@ -288,6 +290,21 @@ call.on('status', (status: grpcWeb.Status) => { }); ``` +(See [here](https://github.com/grpc/grpc-web/blob/4d7dc44c2df522376394d3e3315b7ab0e010b0c5/packages/grpc-web/index.d.ts#L29-L39) full list of possible `.on(...)` callbacks) + +### (Option) Using Promises (Limited features) + +NOTE: It is not possible to access the `.on(...)` callbacks (e.g. for `metadata` and `status`) when Promise is used. + +```ts +this.echoService.echo(request, {'custom-header-1': 'value1'}) + .then((response: EchoResponse) => { + console.log(`Received response: ${response.getMessage()}`); + }).catch((err: grpcWeb.RpcError) => { + console.log(`Received error: ${err.code}, ${err.message}`); + }); +``` + For the full TypeScript example, see [ts-example/client.ts](net/grpc/gateway/examples/echo/ts-example/client.ts) with the [instructions](net/grpc/gateway/examples/echo/ts-example) to run.