Skip to content

Instantly share code, notes, and snippets.

@DanMeakin
Created August 31, 2018 10:55
Show Gist options
  • Save DanMeakin/cc5a504c92000b0c6264ddc10d368a42 to your computer and use it in GitHub Desktop.
Save DanMeakin/cc5a504c92000b0c6264ddc10d368a42 to your computer and use it in GitHub Desktop.
// 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