Document on how to use Promises (#1357)

This commit is contained in:
Eryu Xia 2023-08-30 17:57:40 -07:00 committed by GitHub
parent 4d7dc44c2d
commit 9cb8524caa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -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.