I promises and I cannot find anything that handles my scenario.
I used a sample set of promises that work perfect until I add the pouch allDocs call in.
It works fine when I chain directly on the allDocs call. I was told I could put a promise in a promise but that does not seem to be the case.
The reason I have it set up the way I do and why it would be difficult to use Promise.All() is because I don't want the promises executing all at onece. I want one promise to follow another.
I stripped everything out only showing one promise that has PouchDB call (allDocs) in it. Maybe this cannot be done.
Here is my promise with console.logs in them so you can see the path.
My promise:
let cleanRoom = function () {
return new Promise(function (resolve, reject) {
console.log("starting cleanRoom");
console.log("starting DB_WorkIssue.alldocs");
DB_WorkIssues.allDocs({ include_docs: true, descending: false }, function (err, response) {
data = response.rows;
itemp = 0;
console.log("at For Loop in allDocs");
for (var i = 0; i < response.total_rows; i++) {
if (data[i].doc.IsDeleted || data[i].doc.IsWorkIssueInserted || data[i].doc.IsWorkIssueUpdated || data[i].doc.IsLogInserted) {
DirtyFlag = true;
}
}
console.log("exiting allDocs");
return;
}).then(function () {
console.log("inside then function after alldocs");
}).catch(function (err) {
console.log("inside catch");
showMsg("Error in cleanRoom/allDocs: " + err);
});
console.log("exiting cleanRoom")
resolve('Cleaned The Room');
});
};
All it does is call the allDocs method and then in the function look to see if any records have been updated. Then it can continue on.
But that is not what is happening.
Here is the chain:
}).then(function () {
return initialize();
}).then(function (prMessage) {
**return cleanRoom();** <--- my promise
}).then(function (result) {
return removeGarbage(result);
}).then(function (result) {
return winIcecream(result);
}).then(function (result) {
console.log('finished ' + result);
}).then(function () {
console.log("starting UpdateworkIssuesJson");
return updateWorkIssuesJson();
}).then(function () {
the results:
tasksmain.js:165 starting cleanRoom
tasksmain.js:166 starting DB_WorkIssue.alldocs <-- Pouch call
tasksmain.js:188 exiting cleanroom <-- exits promise
tasksmain.js:195 inside removeGarbage <-- I don't want it to do this until Pouch call is done.
tasksmain.js:202 inside winIceCream
taskspouch.js:91 finished Cleaned The Room remove Garbage won Icecream
taskspouch.js:93 starting UpdateworkIssuesJson
tasksmain.js:182 inside then function after alldocs <-- now back to the pouch function call (way too late.
tasksmain.js:171 at For Loop in allDocs
tasksmain.js:178 exiting allDocs <--- Pouches chain
taskspouch.js:96 starting showTasks
The question is - the original promise chain is working as it is supposed to.
It continues on when the "cleanroom" resolves. But the cleanroom resolves before the Pouch command is finished. It actually finishes when the allDoc
Is there a way to prevent that?