Considering a simple function that should load a .json
file dynamically and conditionally, then return a specific key from this file as below:
export const translate = async (key: string, from: string) =>
await import(`../locales/www/${from}.json`)
.then((translations) => {
const translation = translations.default[key];
console.log(translation); // console.log() works
// Promise.resolve(translation);
return translation; // returns a promise object not the value
})
.catch((err) => console.log(err));
// In another file
translate(a, b)
I can't understand why it doesn't return a value (wanted behaviour) but a Promise.