0

Similarly to this question, I am in need of the following:

// foo.js

import { someAsyncFunc } from './funcs.js';

export default {
    ..., // a bunch of preset properties
    someProp: await someAsyncFunc()
}

Now I know to use await we must be in the async scope, unless using top-level which I cannot given the project configuration. In the linked question, the solution given is as such:

const promise1 = new Promise(function(resolve, reject) {
  resolve('myvalue');
});
const a = async() => {
  var b = await promise1
  return b;
}

const printData = async() => {
  const newobj = {
    'a': await a()
  };
  console.log(newobj)
}

printData()

Which does in fact resolve to const newobj = { ... } but only outputs to the console; the problem I'm trying to solve is getting that newobj out of the printData() call, back to the module level so that it can be exported.

All of my attempts from many SO posts, and attempts using IIFE's at the top-level have all resulted in returned Promise objects as expected from async functions.

Is there a way to get that final object out of the async scope so that it can be exported?

pstatix
  • 3,611
  • 4
  • 18
  • 40
  • Importing is synchronous, it won't wait for the asynchronous function. You should return the promise, and use `await` in the program that imports. – Barmar Apr 17 '23 at 20:40

0 Answers0