admin管理员组文章数量:1399278
I have this async code function to get lang var from AsyncStorage. And I want to call this function on every app page in construct(?) method. But when I try to call it, it looks like the code run synchronous, and my loadedLang is = { _45: 0, _81: 0, _65: null, _54: null }. How to do it in right way? Thanks.
main.js
import load from "./ponents/languageLoad"
constructor(props) {
super(props);
let loadedLang = load();
console.log("LOADED", loadedLang);
this.state = {
settings: 1,
deviceLocale: "EMPTY"
};
}
languageLoad.js
import React, { Component } from 'react';
import { AsyncStorage} from 'react-native';
import Lang from 'react-native-i18n'
export default load = async () => {
try {
const customLang = await AsyncStorage.getItem('customLang');
if (customLang !== null && customLang !== undefined && customLang !== "") {
deviceLocale = customLang;
}
} catch (error) {
deviceLocale = Lang.locale.split("-")[0] || "uk";
}
console.log("------------", deviceLocale, '-------------');
console.log("------------", Lang.t('aboutAppText') , '-------------');
Lang.locale = deviceLocale;
return Lang;
}
I have this async code function to get lang var from AsyncStorage. And I want to call this function on every app page in construct(?) method. But when I try to call it, it looks like the code run synchronous, and my loadedLang is = { _45: 0, _81: 0, _65: null, _54: null }. How to do it in right way? Thanks.
main.js
import load from "./ponents/languageLoad"
constructor(props) {
super(props);
let loadedLang = load();
console.log("LOADED", loadedLang);
this.state = {
settings: 1,
deviceLocale: "EMPTY"
};
}
languageLoad.js
import React, { Component } from 'react';
import { AsyncStorage} from 'react-native';
import Lang from 'react-native-i18n'
export default load = async () => {
try {
const customLang = await AsyncStorage.getItem('customLang');
if (customLang !== null && customLang !== undefined && customLang !== "") {
deviceLocale = customLang;
}
} catch (error) {
deviceLocale = Lang.locale.split("-")[0] || "uk";
}
console.log("------------", deviceLocale, '-------------');
console.log("------------", Lang.t('aboutAppText') , '-------------');
Lang.locale = deviceLocale;
return Lang;
}
Share
Improve this question
edited Apr 30, 2017 at 17:55
Youssef Bouhjira
1,6413 gold badges23 silver badges39 bronze badges
asked Apr 30, 2017 at 16:58
SERGSERG
3,9819 gold badges50 silver badges97 bronze badges
1 Answer
Reset to default 5async
functions need to be called with await
and can only be called from inside of async functions.
JS constructors can't be asynchronous. Therefore you will have restructure your code to work differently. Either load the data outside of a constructor, or have an anonymous self executing async function in the constructor that will load the data and store it. Of course the rest of the class will need to work with the fact that the data might not be there.
本文标签: javascriptHow to call async function from importStack Overflow
版权声明:本文标题:javascript - How to call async function from import? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744205779a2595181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论