I want to do the following:
const contextSeeder = new ContextSeeder(context);
let props = (await (await contextSeeder.withList()).withUser()).get();
But ideally this would look like:
const contextSeeder = new ContextSeeder(context);
let props = await contextSeeder.withList().withUser().get();
withList()
and withUser()
will both make an API call. So ideally they should run in parallel. They both modify an object in the class which is the return value (could there be a race condition?)
So given that I call the two "loaders", I'd want the .get()
to be the one that must actually be waited for.
Is it possible? Because in the first way it looks like it will handle one by one (because of the await in both calls)
I run this inside an async function
, and I need this data to be present by the time the function returns. I can't do a .then()
Thanks
export default class ContextSeeder {
context = {};
serverContext;
constructor(serverContext = undefined) {
this.serverContext = serverContext;
}
withUser = async (listId = undefined) => {
let userContext = {};
await UserContextSeeder(this.serverContext).then(context => {userContext = context});
this.context = {...this.context, user: userContext}
return this;
}
withList = async (userId = undefined) => {
let listContext = {};
await ListContextSeeder(context).then(context => {listContext = context});
this.context = {...this.context, list: listContext}
return this;
}
get = () => {
return this.context;
}
}