Created
August 31, 2018 10:55
-
-
Save DanMeakin/cc5a504c92000b0c6264ddc10d368a42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Transforms an object containing arbitrary keys, and promise values, into a | |
// promise-wrapped object, with the same keys and the result of resolving each | |
// promise as values. | |
const promiseAllObject = (obj) => { | |
const keys = Object.keys(obj); | |
const values = keys.map((k) => obj[k]); | |
return Promise.all(values).then((promises) => | |
promises.reduce((all, promise, idx) => ({ ...all, [keys[idx]]: promise }), {}) | |
); | |
} | |
const fetchTrustBoxData = ( | |
{ businessUnitId, locale, translationFallbacks, theme = 'light', ...opts }, | |
constructTrustBox | |
) => { | |
const baseDataPromise = ... | |
const translationsPromise = ... | |
const readyPromise = ... | |
return promiseAllObject({ | |
baseData: baseDataPromise, | |
translations: translationsPromise, | |
ready: readyPromise | |
}).then(({ baseData, translations }) => { | |
constructTrustBox({ | |
translations, | |
baseData, | |
locale, | |
}); | |
completeLoading(theme, baseData); | |
}) | |
.catch(() => errorFallback()) | |
.finally(() => removeLoader()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment