I have some code that is wrapped in a promise
. This code goes and edits some records in a remote DB and returns with the number of edited records. If the number of edited records is larger than 0
(i.e., some work has been done), then I'd like to run the same method again and again until I get a 0
back (i.e., no more records need editing).
I've tried some different ways, but they all do not terminate (i.e., the resolve is not called) or they don't run properly.
First version I tried:
const doEdits = () => {
return new Promise((resolve, reject) => {
someDB.performEdits()
.then(count => {
if (count > 0) {
doEdits()
} else {
resolve()
}
})
.catch(reject)
})
}
return new Promise((resolve, reject) => {
someDB.init()
.then(doEdits)
.catch(reject)
})
Second version I tried:
const doEdits = () => {
return new Promise((resolve, reject) => {
someDB.performEdits()
.then(count => {
resolve(count)
})
.catch(reject)
})
}
return new Promise((resolve, reject) => {
someDB.init()
.then(doEdits)
.then(count => {
if (count > 0 ) {
return doEdits() // 'doEdits()' did not work...
} else {
resolve()
}
})
.catch(reject)
})
Any and all suggestions are welcome.