Skip to content

Commit 9cb8524

Browse files
authored
Document on how to use Promises (#1357)
1 parent 4d7dc44 commit 9cb8524

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ code.
269269
* `echo_pb.d.ts` - Generated by `--grpc-web_out`, contains TypeScript
270270
definitions for `echo_pb.js`.
271271

272+
### Using Callbacks
273+
272274
```ts
273275
import * as grpcWeb from 'grpc-web';
274276
import {EchoServiceClient} from './EchoServiceClientPb';
@@ -288,6 +290,21 @@ call.on('status', (status: grpcWeb.Status) => {
288290
});
289291
```
290292

293+
(See [here](https://github.com/grpc/grpc-web/blob/4d7dc44c2df522376394d3e3315b7ab0e010b0c5/packages/grpc-web/index.d.ts#L29-L39) full list of possible `.on(...)` callbacks)
294+
295+
### (Option) Using Promises (Limited features)
296+
297+
NOTE: It is not possible to access the `.on(...)` callbacks (e.g. for `metadata` and `status`) when Promise is used.
298+
299+
```ts
300+
this.echoService.echo(request, {'custom-header-1': 'value1'})
301+
.then((response: EchoResponse) => {
302+
console.log(`Received response: ${response.getMessage()}`);
303+
}).catch((err: grpcWeb.RpcError) => {
304+
console.log(`Received error: ${err.code}, ${err.message}`);
305+
});
306+
```
307+
291308
For the full TypeScript example, see
292309
[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.
293310

0 commit comments

Comments
 (0)