admin管理员组文章数量:1429708
Is it possible to use async/await outside of classes? For example I use AsyncStorage to store access token and want to get this token before StackNavigator will be initialized.
container.js
import React from 'react';
import { StackNavigator } from 'react-navigation';
import PairingView from '../ponents/PairingView';
import MainView from '../ponents/MainView';
import { getTokenFromStorageAsync } from '../helpers/asyncStorageHelper';
const accessToken = getTokenFromStorageAsync().done();
console.log(accessToken); <---------- undefined
const AppNavigator = StackNavigator({
PairingRoute: {
screen: PairingView
},
MainRoute: {
screen: MainView
}
}, {
initialRouteName: (accessToken == null) ? 'PairingRoute' : 'MainRoute',
initialRouteParams: {
token: accessToken
}
});
const App = () => (
<AppNavigator />
);
export default App;
asyncStorageHelper.js
import { AsyncStorage } from 'react-native';
export const getTokenFromStorageAsync = async () => {
try {
const value = await AsyncStorage.getItem('@nfs:token');
console.log(value); <---------- access token
if (value != null)
return value;
} catch (err) {
console.error(err);
}
return undefined;
};
Is it possible to use async/await outside of classes? For example I use AsyncStorage to store access token and want to get this token before StackNavigator will be initialized.
container.js
import React from 'react';
import { StackNavigator } from 'react-navigation';
import PairingView from '../ponents/PairingView';
import MainView from '../ponents/MainView';
import { getTokenFromStorageAsync } from '../helpers/asyncStorageHelper';
const accessToken = getTokenFromStorageAsync().done();
console.log(accessToken); <---------- undefined
const AppNavigator = StackNavigator({
PairingRoute: {
screen: PairingView
},
MainRoute: {
screen: MainView
}
}, {
initialRouteName: (accessToken == null) ? 'PairingRoute' : 'MainRoute',
initialRouteParams: {
token: accessToken
}
});
const App = () => (
<AppNavigator />
);
export default App;
asyncStorageHelper.js
import { AsyncStorage } from 'react-native';
export const getTokenFromStorageAsync = async () => {
try {
const value = await AsyncStorage.getItem('@nfs:token');
console.log(value); <---------- access token
if (value != null)
return value;
} catch (err) {
console.error(err);
}
return undefined;
};
Share
Improve this question
edited Jun 26, 2017 at 7:01
R. R.
asked Jun 23, 2017 at 10:16
R. R.R. R.
311 gold badge1 silver badge8 bronze badges
4
- 1 const accessToken = await getTokenFromStorageAsync(); – ponury-kostek Commented Jun 23, 2017 at 10:21
- @ponury-kostek await can be used only with async function – R. R. Commented Jun 23, 2017 at 10:26
- Sorry my mistake. So change it to getTokenFromStorageAsync().then(token => console.log(token)); – ponury-kostek Commented Jun 23, 2017 at 10:28
-
async/await
is part of this year's release (ES2017) not of last years release (ES7/ES2016). Please read tag descriptions. – Felix Kling Commented Jun 25, 2017 at 19:13
2 Answers
Reset to default 2Solved the problem.
class App extends Component {
constructor(props) {
super(props);
this.state = {
accessToken: 'fetching'
};
this._loadAccessToken();
}
_loadAccessToken = async () => {
const token = await getTokenFromStorageAsync();
this.setState({ accessToken: token });
}
render() {
if (this.state.accessToken === 'fetching')
return null;
const AppNavigator = StackNavigator({
PairingRoute: {
screen: PairingView
},
MainRoute: {
screen: MainView
}
}, {
initialRouteName: (this.state.accessToken == null) ? 'PairingRoute' : 'MainRoute',
initialRouteParams: {
token: this.state.accessToken
}
});
return <AppNavigator />;
}
}
export default App;
getTokenFromStorageAsync().then(accessToken => {
console.log(accessToken);
// ...
});
or
// ...
export default (async () => {
const accessToken = await getTokenFromStorageAsync();
console.log(accessToken);
const AppNavigator = StackNavigator({
PairingRoute: {
screen: PairingView
},
MainRoute: {
screen: MainView
}
}, {
initialRouteName: (accessToken == null) ? 'PairingRoute' : 'MainRoute',
initialRouteParams: {
token: accessToken
}
});
const App = () => (
<AppNavigator />
);
return App;
})()
As you can see you have to export a promise that resolves with your App, instead of exporting your App directly.
Edit:
import { AppRegistry } from 'react-native';
import App from './app/container';
(async () => {
let ResolvedApp = await App()
AppRegistry.registerComponent('someappname', () => ResolvedApp);
// ...
})()
本文标签: javascriptReact Nativeasyncawait does not workStack Overflow
版权声明:本文标题:javascript - React Native - asyncawait does not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745541192a2662501.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论