Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: "Cannot use keyword 'await' outside an async function", why? #941

Closed
frederikhors opened this issue Apr 9, 2021 · 5 comments
Closed

Comments

@frederikhors
Copy link
Contributor

Describe the bug
In ./generated/index.ts I'm using this code:

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

let playerService: PlayerServiceClient;

const playerClient = await import('./generated/player.client');

export const player = (): PlayerServiceClient =>
	playerService ||
	((playerService = new playerClient.PlayerServiceClient({})), playerService);

Logs
Cannot use keyword 'await' outside an async function

Expected behavior
I would like to use this because I need lazy load of many files and I would like to use it like this in my components:

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

let players: Player[];

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

Information about your SvelteKit Installation:

Diagnostics
  • The output of npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers:

    System:
    OS: Windows 10 10.0.19042
    Binaries:
    Node: 14.16.1 - C:\Program Files\nodejs\node.EXE
    npm: 7.8.0 - C:\Program Files\nodejs\npm.CMD
    npmPackages:
    @sveltejs/kit: 1.0.0-next.71 => 1.0.0-next.71
    svelte: 3.37.0 => 3.37.0
    vite: 2.1.5 => 2.1.5

  • Your browser: Chrome

  • Your adapter: static

@dymoo
Copy link

dymoo commented Apr 9, 2021

Why not just

export default async function() {
let playerService: PlayerServiceClient;

const playerClient = await import('./generated/player.client');

return () = PlayerServiceClient =>
	playerService ||
	((playerService = new playerClient.PlayerServiceClient({})), playerService);
}

@frederikhors
Copy link
Contributor Author

Because I think is the same. I have double await in components, right?

I would like to have only one await.

How do you call that in components?

@Conduitry
Copy link
Member

The top-level await proposal hasn't reached stage 4 (accepted) and isn't supported by Acorn, the parser we're using.

@frederikhors
Copy link
Contributor Author

@Conduitry I read here that there is an option for acorn: allowAwaitOutsideFunction.

How can I set this option in my svelte kit environment?

@frederikhors
Copy link
Contributor Author

frederikhors commented Apr 10, 2021

I think with this I can write this code:

const playerService = await player();
const { response } = await playerService.queryPlayers({ playerId: "1" });

if (response) {
  players = response.players;
}

instead of:

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

  if (response) {
    players = response.players;
  }
})();

Right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants