1

code

const config = async () => {
  return await import("../test");
}

console.log(config);

../test

export const config = {
  value1: 1,
  value1: 2,
};

I want config in ../test but this returns [AsyncFunction: config].

1
  • const config = await import("../test") will return what you want. async (...) => { ... } is literally the definition of an async function. Commented Jan 21, 2022 at 4:49

1 Answer 1

2

async () => {} is a function declaration. So you assigned config to a function.

You have to call that function, and await the result, in order to get the value you want.

console.log(await config());

But are you sure you want a dynamic import here?

Because this should do the same thing without any headaches at all.

import { config } from "../test";
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Buit I got this error Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher. I think I can't use await.
To use top level await you have to change your tsconfig as instructed by that error message. But you should be able to use await from inside any function without making those changes.
I would like to use a dynamic import

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.