我在下面有一个简单的函数,您可以在其中传递一个String作为参数,该参数将基于Firestore子集合查询返回一个Promise,该Promise将只解析为空对象或填充的对象。
问题是即使我对前一条语句调用了await,最后一个resolve({})
也被调用了,可能会发生什么?
getSubCollections = (id: string): Promise<any> => {
const subCollectionPromise = new Promise(async (resolve: Function) => {
if (typeof this.subCollectionKeys === 'undefined') {
resolve({});
}
const collections = {};
await this.subCollectionKeys.forEach(async (key: string) => {
const subCollection = await this.getCollection()
.doc(id)
.collection(key)
.get();
const firebaseObj = await subCollection.docs.map((obj: Object) =>
this.getShallowData(obj),
);
const fromFirebase = fromFirebaseObj(firebaseObj);
if (!fromFirebase.empty) {
collections[key] = fromFirebase;
}
resolve(collections);
});
resolve({}); /// WHY IS THIS CALLED EVEN IF THERE IS AWAIT ON TOP???
});
return subCollectionPromise;
};