Using node.js mongodb
native module I'm trying to use a promisified iterator but it is not waiting for the promise chain to resolve before firing the finalCallback
db.collection('items').find({}).forEach(promiseIterator, finalCallback);
function promiseIterator(doc){
return Promise.resolve(doc)
.then(res => {
console.log(res); // this fires *after* finalCallback fires
})
}
function finalCallback(res){
console.log(res); // this fires *before* promiseIterator resolves all promise chains
}
The doc is here: https://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#~resultCallback
Their ES6 examples all use generators so I'm not sure if Promises even work here. But the promise chain is not fully resolved before firing finalCallback
.
https://mongodb.github.io/node-mongodb-native/2.2/reference/ecmascript6/crud/
The reason I'm having this issue is because my promiseIterator
needs to make several async/promisified calls on each document. (I am unable to load all docs into memory with .toArray()
as there are over a million documents and I get a process out of memory
error when I try that.