Skip to content

Commit

Permalink
Turn Promise.all to allSettleds
Browse files Browse the repository at this point in the history
  • Loading branch information
fongsean committed Jun 11, 2024
1 parent e924f92 commit 2df6b02
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
4 changes: 2 additions & 2 deletions apps/smart-forms-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"homepage": "https://github.com/aehrc/smart-forms#readme",
"dependencies": {
"@aehrc/sdc-assemble": "^1.2.0",
"@aehrc/sdc-populate": "^2.2.0",
"@aehrc/smart-forms-renderer": "^0.35.0",
"@aehrc/sdc-populate": "^2.2.1",
"@aehrc/smart-forms-renderer": "^0.35.1",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@fontsource/material-icons": "^5.0.16",
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sdc-populate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aehrc/sdc-populate",
"version": "2.2.0",
"version": "2.2.1",
"description": "Performs the $populate operation from the HL7 FHIR SDC (Structured Data Capture) specification: http://hl7.org/fhir/uv/sdc",
"main": "lib/index.js",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,19 @@ async function populateReferenceContextsIntoContextMap(
try {
// Resolve promises for referenceContextsTuple
const promises: Promise<any>[] = referenceContextTuples.map(([, promise]) => promise);
const responses = await Promise.all(promises);
const resources: (FhirResource | null)[] = responses.map((response) =>
responseDataIsFhirResource(response?.data) ? (response.data as FhirResource) : null
);
const settledPromises = await Promise.allSettled(promises);
const resources: (FhirResource | null)[] = settledPromises.map((settledPromise) => {
if (settledPromise.status === 'rejected') {
return null;
}

const response = settledPromise.value;
if (responseDataIsFhirResource(response?.data)) {
return response.data as FhirResource;
}

return null;
});

// Update referenceContextTuples with resolved resources
referenceContextTuples = referenceContextTuples.map((tuple, i) => {
Expand Down Expand Up @@ -179,10 +188,19 @@ async function populateBatchContextsIntoContextMap(

// Resolve promises for batchContextEntryTuples
const promises: Promise<any>[] = batchContextEntryTuples.map(([, promise]) => promise);
const responses = await Promise.all(promises);
const resources: (FhirResource | null)[] = responses.map((response) =>
responseDataIsFhirResource(response?.data) ? (response.data as FhirResource) : null
);
const settledPromises = await Promise.allSettled(promises);
const resources: (FhirResource | null)[] = settledPromises.map((settledPromise) => {
if (settledPromise.status === 'rejected') {
return null;
}

const response = settledPromise.value;
if (responseDataIsFhirResource(response?.data)) {
return response.data as FhirResource;
}

return null;
});

// Add resources to batch bundle
for (let i = 0; i < resources.length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions packages/smart-forms-renderer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aehrc/smart-forms-renderer",
"version": "0.35.0",
"version": "0.35.1",
"description": "FHIR Structured Data Captured (SDC) rendering engine for Smart Forms",
"main": "lib/index.js",
"scripts": {
Expand All @@ -27,7 +27,7 @@
},
"homepage": "https://github.com/aehrc/smart-forms#readme",
"dependencies": {
"@aehrc/sdc-populate": "^2.2.0",
"@aehrc/sdc-populate": "^2.2.1",
"@iconify/react": "^4.1.1",
"dayjs": "^1.11.10",
"deep-diff": "^1.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,14 @@ export async function resolveLookupPromises(
const lookupPromiseValues = Object.values(codeSystemLookupPromises);

const promises = lookupPromiseValues.map((lookupPromise) => lookupPromise.promise);
const lookupResults = await Promise.all(promises);
const settledPromises = await Promise.allSettled(promises);

for (const [i, lookupResult] of lookupResults.entries()) {
for (const [i, settledPromise] of settledPromises.entries()) {
if (settledPromise.status === 'rejected') {
continue;
}

const lookupResult = settledPromise.value;
if (!lookupResponseIsValid(lookupResult)) {
continue;
}
Expand Down

0 comments on commit 2df6b02

Please sign in to comment.