mirror of https://github.com/grpc/grpc-web.git
Document on how to use Promises (#1357)
This commit is contained in:
parent
4d7dc44c2d
commit
9cb8524caa
17
README.md
17
README.md
|
@ -269,6 +269,8 @@ code.
|
||||||
* `echo_pb.d.ts` - Generated by `--grpc-web_out`, contains TypeScript
|
* `echo_pb.d.ts` - Generated by `--grpc-web_out`, contains TypeScript
|
||||||
definitions for `echo_pb.js`.
|
definitions for `echo_pb.js`.
|
||||||
|
|
||||||
|
### Using Callbacks
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import * as grpcWeb from 'grpc-web';
|
import * as grpcWeb from 'grpc-web';
|
||||||
import {EchoServiceClient} from './EchoServiceClientPb';
|
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
|
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.
|
[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.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue