0

I have an index.js which gets data from a json file.
I export this function to my App.js where I want to store this data in another object.

configurations/index.js:

let config = (async () => {
    let data;
    if (process.env.NODE_ENV !== 'development') {
        if (
            typeof window !== 'undefined' &&
            (window.location.hostname.match('localhost') || window.location.hostname.match('127.0.0.1'))
        ) {
            data = require('./config.client.local.json');
        } else {
            data = require('./config.client.ip.json');
        }
    } else {
        data = fetch(process.env.PUBLIC_URL + "/config.client.json")
            .then(async res => await res.json());
    }
    return Object.assign({}, await data);
})();

export default config;

App.js

 import OIDC from 'oidc-client';
 import config from './configurations';
 window["UserManager"] = new OIDC.UserManager(config.oidc);

I have no idea how to get the data without using async/await

Does anyone have an idea?

MrLine
  • 638
  • 7
  • 25
  • 1
    `.then(res => res.json());` [don't mix async/await with Promises](https://stackoverflow.com/a/54387912/1218980), it's redundant and confusing. – Emile Bergeron Jul 26 '19 at 14:26
  • Also, your config is a promise, even if you use `async/await`, so you either use `config.then(realConfigData => /* */)` or `await config`. – Emile Bergeron Jul 26 '19 at 14:29
  • @EmileBergeron, is there a way so I can fetch this file(file is outside src) without using fetch? – MrLine Jul 26 '19 at 14:33
  • Depending on your project's configuration, there are chances that you can just `import jsonConfig from '../config.client.json';` and parse this once at build time. – Emile Bergeron Jul 26 '19 at 14:36

1 Answers1

3

It looks like config is a Promise which means that you can do this:

config.then(({ oidc }) => {
  window.UserManager = new OIDC.UserManager(oidc);
});
Titus
  • 22,031
  • 1
  • 23
  • 33