[英]How to use Async/Await in this case?
I have an index.js which gets data from a json file. 我有一个index.js,它从json文件获取数据。
I export this function to my App.js where I want to store this data in another object. 我将此函数导出到我的App.js中,我想在其中将数据存储在另一个对象中。
configurations/index.js: 配置/ 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 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? 有人有主意吗?
It looks like config
is a Promise
which means that you can do this: 看起来
config
是一个Promise
,这意味着您可以执行以下操作:
config.then(({ oidc }) => {
window.UserManager = new OIDC.UserManager(oidc);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.