Typescript 如何在gRPC Web上使用异步/等待模式?
我正在开发一个Vue.js web应用程序,我正在使用gRPC来确保通信 在前端,我使用的是gRPC的web版本:Typescript 如何在gRPC Web上使用异步/等待模式?,typescript,vue.js,async-await,grpc,grpc-web,Typescript,Vue.js,Async Await,Grpc,Grpc Web,我正在开发一个Vue.js web应用程序,我正在使用gRPC来确保通信 在前端,我使用的是gRPC的web版本:gRPC web 一切正常;但是,当我在商店中进行API调用时,例如: async fetchEcho() { const echoService = new EchoServiceClient('http://localhost:8080', null, null); const request = new EchoRequest(); request.s
gRPC web
一切正常;但是,当我在商店中进行API调用时,例如:
async fetchEcho() {
const echoService = new EchoServiceClient('http://localhost:8080', null, null);
const request = new EchoRequest();
request.setMessage('Hello World!');
const call = echoService.echo(request, {'custom-header-1': 'value1'},
(err: grpcWeb.Error, response: EchoResponse) => {
console.log(response.getMessage());
});
}
在我的Vue文件中进行调用时,如何使用async/await
模式,例如:
await fetchEcho();
// ...
并等待API调用完成后继续我的程序
谢谢 我使用的是ServicePromiseClient,示例如下:
const echoServiceClient = new EchoServicePromiseClient("http://localhost:8080", null, null);
async function fetchEcho() {
const echoReq = new EchoRequest();
echoReq.setMessage("Hello world!");
try {
const response = await echoServiceClient.echo(echoReq, {});
return response;
} catch (error) {
console.log(error);
return null;
}
}
然后,我可以创建异步函数来进行如下提取:
const echoServiceClient = new EchoServicePromiseClient("http://localhost:8080", null, null);
async function fetchEcho() {
const echoReq = new EchoRequest();
echoReq.setMessage("Hello world!");
try {
const response = await echoServiceClient.echo(echoReq, {});
return response;
} catch (error) {
console.log(error);
return null;
}
}
wait
与承诺一起使用。是否有一种形式的echo
返回承诺而不是调用回调?什么是EchoServicePromiseClient(?)@dmaixner@MarlonLópez alternative(promise)客户端,基于本教程: