I am trying to sync some database data with local data. Specifically, I want to 'load' a LinkTable from the database (the load function, itself, handles the merging of the data) and then, AFTER the load function I want to push the LinkTable to the database. When done, the desired outcome would be that the database and the local versions of the LinkTable would be identical. To complicate matters, the load function can reject in ways that are acceptable... and I need to continue down the promise chain if that is the case.
Lastly, after the syncing of the LinkTable... I need to do additional tasks that rely on said LinkTable.
I'm using react/redux but the question is more about promises.
The relevant (failing) code looks like this:
dispatch(loadLinkTableFromDB(username))
.then((successLoadLinkTableMsg) => {
console.log('Successfully loaded link table: ', successLoadLinkTableMsg)
return dispatch(pushLinkTableToDB(username))
})
.catch((rejectLoadLinkTableReason) => {
console.log("Failed to load link table from DB: " + rejectLoadLinkTableReason);
if (allReasonsAcceptableForOverwrite(rejectLoadLinkTableReason)) { // some rejection reasons are accectable... so if failed reason is okay....
return dispatch(pushLinkTableToDB(username));
} else {
// console.log("Rejecting: ", rejectLoadLinkTableReason);
return Promise.reject(rejectLoadLinkTableReason);
}
})
.catch((unacceptableRejectionReasons) => {
console.log('unacceptableRejectionReasons :', unacceptableRejectionReasons);
})
.then(( *do more stuff that relies on the LinkTable *))
My issue is that a an unacceptable rejection in loading the LinkTable... AND a rejection of any kind in pushing the link table from the first catch end up being caught by the second catch. When it's an unacceptable rejection, I want the entire promise chain to end. When it's an acceptable rejection and then a subsequent rejection in the pushLinkTableToDB... I want the promise chain to continue.