Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Javascript 如何将此代码从使用双等待转换为延迟加载导入的单等待_Javascript_Typescript_Refactoring_Lazy Evaluation_Lazy Initialization - Fatal编程技术网

Javascript 如何将此代码从使用双等待转换为延迟加载导入的单等待

Javascript 如何将此代码从使用双等待转换为延迟加载导入的单等待,javascript,typescript,refactoring,lazy-evaluation,lazy-initialization,Javascript,Typescript,Refactoring,Lazy Evaluation,Lazy Initialization,我使用的是Svelte和Protobuf生成的客户端 在/generated/index.ts中,我使用以下代码: import type { PlayerServiceClient } from './proto/player.client'; const transport = new GrpcWebFetchTransport({ baseUrl: 'http://localhost:3000/api' }); let playerService: PlayerServiceClien

我使用的是Svelte和Protobuf生成的客户端

/generated/index.ts
中,我使用以下代码:

import type { PlayerServiceClient } from './proto/player.client';

const transport = new GrpcWebFetchTransport({ baseUrl: 'http://localhost:3000/api' });

let playerService: PlayerServiceClient;
export const player = async (): Promise<PlayerServiceClient> =>
    playerService ||
    ((playerService = new (await import('./player.client')).PlayerServiceClient(transport)),
    playerService);
问题

有没有办法在每个组件中仍然使用lazy
import()
,但不使用verbose-double
wait

我希望使用以下代码:

import { player } from "../generated";

let players: Player[];

(async () => {
  players = await player.queryPlayers({ playerId: "1" });
})();
附加上下文


我有许多不同的类型(以及相应的服务)(数百种)和许多不同的导航路线:这就是我需要延迟加载的原因。

对于顶级
await
您可以只
导出const player=awaitimport { player } from "../generated";

let players: Player[];

(async () => {
  players = await player.queryPlayers({ playerId: "1" });
})();