【发布时间】:2019-07-27 21:47:27
【问题描述】:
我想执行以下操作,并记住它实际上对我有用。我的问题是会发生什么,是否可以从非异步 componentDidMount() func 调用 async usrs() func?
如果不可能通过调用this.usrs(usrs)而不是await this.usrs(usrs);来为我工作
let outState = {};
class A extends Component{
componentDidMount(){
this.usrs(usrs);
}
getData(usr){
return db.collection('a/'+usr+'/bb').get().then(snap=>{
for(i = snap.docs.length-1; i>=0; i--){
outState[usr] = [...outState[usr], snap.docs[i].data()];
if(i === 0) return outState;
}
return false;
});
}
async usrs(usrs){
let os = {}, data = {};
for(i = usrs.length-1; i>=0; i--){
os = await this.getData(usrs[i]);
if(os){
data = { ...data, ...os };
if (i === 0) {
this.setState({ ...this.state, ...data });
}
}
}
}
}
【问题讨论】:
-
您可以在任何异步函数上调用 .then 方法,而不是等待调用
-
作为参考,我刚刚看到这篇文章,它似乎描述了使用和不使用
async的变化:dev.to/codeprototype/…
标签: javascript reactjs react-native google-cloud-firestore