Ok, it’s more clear here.
I can see multiple things you can improve:
- You are mixing calls to
then
and await
, you could simplify everything with only await
- Sometimes you are calling
await
and also Promise.all
on a value that is no more a promise
- Why do you need to use
DS.PromiseArray
? In general this utility is only necessary just before passing a value to a template. Here you are using it internally in your promise handling. You don’t need this. It’s only necessary when passing the result of a promise to the component for displaying it.
Also, I don’t understand how this part is supposed to work:
Promise.all([
stubInternalRecordsPromise,
stubWorkExperiencesPromise
]).then(([internalRecords, qualification, workExperiences]) => {
stub.internalRecords = internalRecords;
stub.workExperiences = workExperiences;
})
As you pass an array of 2 elements and expect an array of 3 in the result.
I imagined something like that, I created functions to make it more decoupled, but could not test if I did not made any typo or naming errors, just to give you the idea
async function loadStub(stub){
const userStub = stub["forest-UserStub"];
const stubInternalRecordsParams = {
filters: JSON.stringify({
aggregator: "and",
conditions: [
{
field: "UserStubID",
operator: "equal",
value: userStub.id,
},
],
}),
"page[number]": 1,
"page[size]": 50,
};
const [stubInternalRecords, stubWorkExperiences] = await Promise.all([
this.store.query("forest-InternalRecord", stubInternalRecordsParams),
this.store.query("forest-WorkExperience", stubInternalRecordsParams),
])
stub.internalRecords = internalRecords;
stub.workExperiences = workExperiences;
return stub;
}
async function loadStubs(stubsParams){
const stubs = await this.store.query("forest-SuggestedTalentStub", stubsParams);
return Promise.all(stubs.map(loadStub));
}
async function loadUsersAndStubs(usersParams, stubsParams){
const [stubs, users] = await Promise.all([
loadStubs(stubsParams),
this.store.query("forest-TalentQueuexUser", userParams),
]);
return [...stubs,... users];
}
// Set up a loading state
set(this, "currentRecordIsLoading", true);
return DS.PromiseArray.create({
promise: loadUsersAndStubs(usersParams, stubsParams).then(
(usersAndStubs) => {
set(this, "currentRecordIsLoading", false);
return usersAndStubs;
}
),
});