Node.js使用Promise.all用于返回异步调用吗?

时间:2018-07-28 05:36:06

标签: node.js firebase firebase-realtime-database promise google-cloud-firestore

当前,我正在使用云功能来查询帖子。当查询帖子时,我在查询帖子的一些其他更新后为firebase设置了一些更新,例如:

 const getPostsForDate = admin.firestore().collection('posts').where('timeOfDeletion', '<', currentTime)
return getPostsForDate.get().then(snapshot => {
    const updates = {} 
    var counter = 0
    const batch = admin.firestore().batch()
    snapshot.forEach((doc) => {

        var key = doc.id
        admin.database().ref('/convoID/' + key).once('value', (snapshot) => {
            if (snapshot.exists()) {
                const convoIDCollection = snapshot.val()
                for (var child in convoIDCollection) {

                    console.log(child)
                    updates["conversations/" + child] = null
                    updates["messages/"+ child] = null
                    updates["convoID/"+ child] = null
                }
            }
            updates["/convoID/" + key] = null
            updates["/reveals/" + key] = null
            updates["/postDetails/" + key] = null
            const postFireStoreRef = admin.firestore().collection('posts').doc(key)
            const posterRef = admin.firestore().collection('posters').doc(key)
            batch.delete(postFireStoreRef)
            batch.delete(posterRef)
            counter++
         })

    })
    if (counter > 0) {
        console.log("at the deletion")
          return Promise.all[admin.database().ref().update(updates), batch.commit()] 
    }
    else {
        console.log("null")
        return null
    }
})

})

但是,问题是查询admin.database()。ref('convoID / ...)是异步的;因此,更新将空发送到数据库,并且没有任何更改。现在,解决这个问题的方法就是promise,除了实现promise。所有其他回报都不如预期。我尝试过

 var promises = []
    snapshot.forEach((doc) => {

        var key = doc.id
        promises.push(admin.database().ref('/convoID/' + key).once('value', (snapshot) => {
            if (snapshot.exists()) {
                const convoIDCollection = snapshot.val()
                for (var child in convoIDCollection) {

                    console.log(child)
                    updates["conversations/" + child] = null
                    updates["messages/"+ child] = null
                    updates["convoID/"+ child] = null
                }
            }
            updates["/convoID/" + key] = null
            updates["/reveals/" + key] = null
            updates["/postDetails/" + key] = null
            const postFireStoreRef = admin.firestore().collection('posts').doc(key)
            const posterRef = admin.firestore().collection('posters').doc(key)
            batch.delete(postFireStoreRef)
            batch.delete(posterRef)
            counter++
         })
        )
    })
promises.all(promises).then(() =>
     if (counter > 0) {
        console.log("at the deletion")
          return Promise.all[admin.database().ref().update(updates), batch.commit()] 
    }
    else {
        console.log("null")
        return null
    }
);

除非我收到错误unexpected token ifDeclaration or statement expected,最后是promises.all(),这是等待异步调用完成的正确方法吗?

1 个答案:

答案 0 :(得分:2)

为解决这个问题,我将尝试总结我们在评论中涉及的内容:

修复箭头功能定义。对此进行更改:

promises.all(promises).then(() => code here)

对此:

Promise.all(promises).then(() => { code here });

然后,修复对Promise.all()的调用。对此进行更改:

return Promise.all[...] 

对此:

return Promise.all([...])

而且,admin.database().ref().once()将返回一个诺言,但前提是您没有通过常规的回调函数.once()

因此,请更改此内容:

promises.push(admin.database().ref('/convoID/' + key).once('value', (snapshot) => { ...}));

对此:

promises.push(admin.database().ref('/convoID/' + key).once('value').then(snapshot => {...}));

而且,如果您更改此代码的一般结构,则会更简洁:

let promises = [];
snapshot.forEach((doc) => {
   promises.push(...)
});
Promise.all(promises).then(...)

对此:

Promise.all(snapshot.map(doc => {
    return admin.database().ref('/convoID/' + key).once(...).then(...);
})).then(...);