From 1bcc156f3cd5bb94c1ac4aa77bc5841dfe59804f Mon Sep 17 00:00:00 2001 From: Quentin Ruhier Date: Fri, 11 Oct 2024 18:45:13 +0200 Subject: [PATCH] fix: handle promises for external resources --- .../core/usecases/synchronizeData/thunks.ts | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/drama-queen/src/core/usecases/synchronizeData/thunks.ts b/drama-queen/src/core/usecases/synchronizeData/thunks.ts index 865f38e6..7f80c024 100644 --- a/drama-queen/src/core/usecases/synchronizeData/thunks.ts +++ b/drama-queen/src/core/usecases/synchronizeData/thunks.ts @@ -204,9 +204,9 @@ export const thunks = { ) // add in cache the missing external resources for needed questionnaires - await Promise.all( - neededQuestionnaires.map((questionnaire) => { - getResourcesFromExternalQuestionnaire( + const prGetExternalResources = Promise.all( + neededQuestionnaires.map(async (questionnaire) => { + await getResourcesFromExternalQuestionnaire( externalResourcesUrl, questionnaire ).then(() => @@ -216,16 +216,24 @@ export const thunks = { ) // delete the cache of every not needed external questionnaires - await Promise.all( + const prDeleteExternalResources = Promise.all( notNeededQuestionnaires.map((questionnaire) => caches.delete(questionnaire.cacheName) ) ) // delete the root-cache of external resources if no external questionnaire is needed - if (neededQuestionnaires.length === 0) { - await caches.delete(externalResourcesRootCacheName) - } + const prDeleteExternalRootCache = + neededQuestionnaires.length === 0 + ? caches.delete(externalResourcesRootCacheName) + : Promise.resolve() + + // We await untill the promises for external resources are finished + await Promise.all([ + prGetExternalResources, + prDeleteExternalResources, + prDeleteExternalRootCache, + ]) } //We await untill all the promises are finished