Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何在gRPC Web上使用异步/等待模式?_Typescript_Vue.js_Async Await_Grpc_Grpc Web - Fatal编程技术网

Typescript 如何在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

我正在开发一个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.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)客户端,基于本教程: