admin管理员组文章数量:1293645
I am trying to build a react app and want to call a function like this:
if (typeof window !== 'undefined') { var token = (await getItem('secure_token')); }
if (typeof window !== 'undefined') { var user_id = (await getItem('user_id')); }
I am getting error that:
Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)
How can I achieve that. because without await, I am getting [object Promise]
and not the actual value.
Below is the plete code:
import axios from "axios";
import { getStorage } from "../../ponents/Helper";
const API_URL = "http://127.0.0.1:8000/api";
const BASE_API = "http://127.0.0.1:8000";
var token = null;
var user_id = null;
if (typeof window !== 'undefined') { var token = (await getItem('secure_token')); }
if (typeof window !== 'undefined') { var user_id = (await getItem('user_id')); }
export const API_ROUTE = axios.create({
baseURL: API_URL,
withCredentials: true,
crossdomain: true,
headers: {
"Key": token,
"Id": user_id
}
});
export const csrf_token = async () => {
await axios.get(BASE_API + '/sanctum/csrf-cookie');
}
async function getItem(item) {
var data = await getStorage(item);
return data;
}
I am trying to build a react app and want to call a function like this:
if (typeof window !== 'undefined') { var token = (await getItem('secure_token')); }
if (typeof window !== 'undefined') { var user_id = (await getItem('user_id')); }
I am getting error that:
Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)
How can I achieve that. because without await, I am getting [object Promise]
and not the actual value.
Below is the plete code:
import axios from "axios";
import { getStorage } from "../../ponents/Helper";
const API_URL = "http://127.0.0.1:8000/api";
const BASE_API = "http://127.0.0.1:8000";
var token = null;
var user_id = null;
if (typeof window !== 'undefined') { var token = (await getItem('secure_token')); }
if (typeof window !== 'undefined') { var user_id = (await getItem('user_id')); }
export const API_ROUTE = axios.create({
baseURL: API_URL,
withCredentials: true,
crossdomain: true,
headers: {
"Key": token,
"Id": user_id
}
});
export const csrf_token = async () => {
await axios.get(BASE_API + '/sanctum/csrf-cookie');
}
async function getItem(item) {
var data = await getStorage(item);
return data;
}
Share
Improve this question
edited Jul 10, 2021 at 8:46
VLAZ
29.1k9 gold badges62 silver badges84 bronze badges
asked Jul 10, 2021 at 8:22
Haren SarmaHaren Sarma
2,5537 gold badges48 silver badges75 bronze badges
5
-
1
Just wrap it in a function and call that function at the top level. e.g
async function main() { /*Do stuff */ }
main()
– TommyBs Commented Jul 10, 2021 at 8:25 - 1 I believe you can only do top-level await in TypeScript, possibly behind a feature flag. – Ace Commented Jul 10, 2021 at 8:32
- Is this in your html file or your script file? Are you including your react bundle after that line and expect those variables to already be there? what es after that that uses the two variables that you are setting? – Alex028502 Commented Jul 10, 2021 at 8:34
- (Can you give us a little more context?) – Alex028502 Commented Jul 10, 2021 at 8:34
- Hi, I have updated the answer with plete code – Haren Sarma Commented Jul 10, 2021 at 8:43
3 Answers
Reset to default 2Currently there is no top level await support present in react and as far as I know only Deno supports top level await. So to overe your problem all you can do is to use IIFE i.e., immediate invoked function expression to solve your problem in the below manner:
var token, user_id;
(async () => {
If (typeof window !== 'undefined') { token = (await getItem('secure_token')); }
if (typeof window !== 'undefined') { user_id = (await getItem('user_id')); }
})()
You can do this to export a function dependent on a promise :
let getTokens = async () => {
var token = null;
var user_id = null;
if (typeof window !== 'undefined') { token = (await getItem('secure_token')); }
if (typeof window !== 'undefined') { user_id = (await getItem('user_id')); }
return {token, user_id };
}
export const API_ROUTE = getTokens().then((data)=> { return axios.create({
baseURL: API_URL,
withCredentials: true,
crossdomain: true,
headers: {
"Key": data.token,
"Id": data.user_id
}
}); });
You can use this promise to access the axios instance in your module.
You can wrap your code in the async function and immediately execute it
var token = null;
var user_id = null;
await ((async () => {
if (typeof window !== 'undefined') { token = (await getItem('secure_token')); }
if (typeof window !== 'undefined') { user_id = (await getItem('user_id')); }
})());
本文标签: javascriptHow can I do top level awaitStack Overflow
版权声明:本文标题:javascript - How can I do top level await - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741584324a2386753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论