0

I have next code and sequence in which it runs is incorrect:

async function getItems(table) {
  return obj; // call 3, why it is called after module.exports and how to fix it
}

const {
var1,
var2
} = (async () => {
  await getItems('env-variables-feature'); // call 1
})();

exports = {
status: var1,
something: var2
};

module.exports = exports; // call 2

As I know await means we should wait until function execute

Ted
  • 1,682
  • 3
  • 25
  • 52
  • I'm not sure the second property desctructuring will work as you expect – evolutionxbox Nov 29 '21 at 14:31
  • 1
    Probably because of the IIFE `(async () => { await getItems('env-variables-feature'); // call 1 })();`. Why do you use an IIFE there? – jabaa Nov 29 '21 at 14:31
  • how to do this in other way? – Ted Nov 29 '21 at 14:32
  • That await seems rather useless here. You can't export something asynchronously. Typically you export the async function itself and have the consumer of the package call the function they imported. – Wyck Nov 29 '21 at 14:33
  • 1
    I'm not sure why people say it doesn't work: https://v8.dev/features/top-level-await – jabaa Nov 29 '21 at 14:34
  • 1
    @jabaa they didn't await the function.. Would have needed `= await (async ...` – Wyck Nov 29 '21 at 14:35
  • 1
    @Wyck That's in my first comment. The IIFE is the problem. Without the IIFE it should work. – jabaa Nov 29 '21 at 14:36
  • @jabaa the IIFE calls an async function though. So there's no way you can have the results of `getItems` by the time you are redy to export from your module. See https://stackoverflow.com/a/38312566/1563833 – Wyck Nov 29 '21 at 14:37
  • @jabaa you mean just call getItems('env-variables-feature'); ? – Ted Nov 29 '21 at 14:37
  • 2
    @Wyck That was 2016. Things have changed since then. Top-level await is supported in Node.js and multiple browsers today. – jabaa Nov 29 '21 at 14:38
  • @Ted `const { var1, var2 } = await getItems('env-variables-feature');` – jabaa Nov 29 '21 at 14:40
  • @jabaa it fails: Cannot use keyword 'await' outside an async function – Ted Nov 29 '21 at 14:41
  • What is your Node.js version? You have to use a current version. – jabaa Nov 29 '21 at 14:41
  • also would have needed to return as in : `await (async () => { return await getItems('env-variables-feature'); })();` It's a bit of a mess. Should just have `async function load() { const { var1, var2 } = await getItems('env-variables-feature'); return { status: var1, something: var2 }; }` and then export `load` – Wyck Nov 29 '21 at 14:42
  • @Wyck You also should update your Node.js version. That's how you would solve it 2 years ago. `const { var1, var2 } = await getItems('env-variables-feature');` at top-level works in the current Node.js version – jabaa Nov 29 '21 at 14:43
  • @jabaa I am using 12.14 and can't change it – Ted Nov 29 '21 at 14:43
  • You should use at least Node.js 16.13.0. That's the current LTS version. Why can't you change it? – jabaa Nov 29 '21 at 14:44
  • @jabaa project requirements – Ted Nov 29 '21 at 14:47
  • 2
    @Ted Node.js 12 doesn't support it. You can export a function that returns a promise and you can await that promise, like Wyck explained in the comments. – jabaa Nov 29 '21 at 14:49
  • I think this is a bad duplicate because the problem hinges on the module import/export mechanism and top-level await, not just how to use an async function or an async IIFE. – Wyck Nov 29 '21 at 18:00

0 Answers0