Javascript 调用构造函数中具有await关键字的函数? 类AuthLoadingScreen扩展了React.Component{ 建造师(道具){ 超级(道具); 这个。_bootstrapAsync(); } //从存储器中取出令牌,然后导航到适当的位置 _bootstrapAsync=async()=>{ const userToken=await AsyncStorage.getItem('userToken'); //这将切换到应用程序屏幕或身份验证屏幕,并进行此加载 //屏幕将被卸下并丢弃。 this.props.navigation.navigate(userToken?'App':'Auth'); }; //在此处呈现您喜欢的任何加载内容 render(){ 返回( ); } }
我知道等待将等待执行,直到承诺解决。在这里,我们正在调用bootstrapAsync函数,它在构造函数中有wait关键字,那么这是否意味着构造函数的执行将等待承诺解决呢?我认为您应该将Javascript 调用构造函数中具有await关键字的函数? 类AuthLoadingScreen扩展了React.Component{ 建造师(道具){ 超级(道具); 这个。_bootstrapAsync(); } //从存储器中取出令牌,然后导航到适当的位置 _bootstrapAsync=async()=>{ const userToken=await AsyncStorage.getItem('userToken'); //这将切换到应用程序屏幕或身份验证屏幕,并进行此加载 //屏幕将被卸下并丢弃。 this.props.navigation.navigate(userToken?'App':'Auth'); }; //在此处呈现您喜欢的任何加载内容 render(){ 返回( ); } },javascript,react-native,react-navigation,react-nagivation,Javascript,React Native,React Navigation,React Nagivation,我知道等待将等待执行,直到承诺解决。在这里,我们正在调用bootstrapAsync函数,它在构造函数中有wait关键字,那么这是否意味着构造函数的执行将等待承诺解决呢?我认为您应该将这个方法从构造函数移动到componentDidMount()生命周期方法 此外,授权用户的方法有很多,最佳做法是为私有路由创建路由组件 我认为您应该将this.\u bootstrapAsync()方法从constructor移动到componentDidMount()生命周期方法 此外,授权用户的方法有很多,最
这个方法从构造函数移动到componentDidMount()
生命周期方法
此外,授权用户的方法有很多,最佳做法是为私有路由创建路由组件 我认为您应该将this.\u bootstrapAsync()
方法从constructor
移动到componentDidMount()
生命周期方法
此外,授权用户的方法有很多,最佳做法是为私有路由创建路由组件 您正在使用的是,AuthLoader
是一个“开关加载程序组件”,它利用了。这是一个您希望花费尽可能少的时间的屏幕
为什么??因为除了一个不确定的进度条外,没有其他内容可以显示。这就是在构造函数中调用引导异步的原因
构造函数执行将等待承诺解决
不,不会的。switch loader的想法是加载应用程序的当前状态并重定向(akaswitch
)到正确的路由
这就是为什么不管屏幕是否已安装,也就是为什么在构造函数中调用bootstrapAsync,因为这是可以调用的最早版本。您使用的是,AuthLoader
是一个“开关加载程序组件”,它利用了。这是一个您希望花费尽可能少的时间的屏幕
为什么??因为除了一个不确定的进度条外,没有其他内容可以显示。这就是在构造函数中调用引导异步的原因
构造函数执行将等待承诺解决
不,不会的。switch loader的想法是加载应用程序的当前状态并重定向(akaswitch
)到正确的路由
这就是为什么屏幕是否被挂载并不重要,这也是为什么在构造函数中调用bootstrapAsync的原因,因为这是可以调用的最早的函数。它不会等待,因为构造函数不是异步函数,在函数调用之前没有等待
为了等待函数结束后执行代码,您需要使用。then()
它不会等待,因为构造函数不是异步函数,并且在函数调用之前没有等待
为了等到函数结束后再执行代码,您需要使用。然后()
不,不会。为了确保在构造函数返回值之前承诺已解决,您应该实际从构造函数返回承诺并等待它
class AuthLoadingScreen extends React.Component {
constructor(props) {
super(props);
this._bootstrapAsync();
}
// Fetch the token from storage then navigate to our appropriate place
_bootstrapAsync = async () => {
const userToken = await AsyncStorage.getItem('userToken');
// This will switch to the App screen or Auth screen and this loading
// screen will be unmounted and thrown away.
this.props.navigation.navigate(userToken ? 'App' : 'Auth');
};
// Render any loading content that you like here
render() {
return (
<View>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
1 2 3 4是输出不,它不会。为了确保在构造函数返回值之前承诺已解决,您应该实际从构造函数返回承诺并等待它
class AuthLoadingScreen extends React.Component {
constructor(props) {
super(props);
this._bootstrapAsync();
}
// Fetch the token from storage then navigate to our appropriate place
_bootstrapAsync = async () => {
const userToken = await AsyncStorage.getItem('userToken');
// This will switch to the App screen or Auth screen and this loading
// screen will be unmounted and thrown away.
this.props.navigation.navigate(userToken ? 'App' : 'Auth');
};
// Render any loading content that you like here
render() {
return (
<View>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
1 2 3 4是输出也许问题是:函数中的异步
是什么?不,构造函数将在承诺解决之前完成;它不会等待结果。@jornsharpe,是构造函数或所有函数的特例会这样做;您调用的函数是异步的,这意味着它返回一个承诺,它不会神奇地使进程同步。也许问题是:函数中的async
是什么?不,构造函数将在承诺解决之前完成;它不会等待结果。@jornsharpe,是构造函数或所有函数的特例会这样做;您调用的异步函数只意味着它返回一个承诺,它不会神奇地使进程同步。您是否意识到这是react-native,您建议的不应该在这种情况下执行?刚刚发布了一个关于异步构造函数常见情况的答案。强烈不建议这样做。您是否意识到这是react-native,您建议的不应该在中执行这种情况?刚刚发布了一个关于异步构造函数常见情况的答案。强烈不建议这样做。
(async () => {
class foo {
constructor() {
console.log(2)
return new Promise(resolve => resolve(this)).then(() => console.log(3))
}
}
console.log(1)
const b = await new foo()
console.log(4)
})()