I am working on a piece of code utilizing Promises and I came to the point where I would like to reject a Promise and have some return values. Essentially in some cases it is not enough to reject the Promise blindly. I have to do some extra work based on what client code i.e. Promise catch statement logic decides to return.
Right now it seems reject does not return any value regardless.
Can anyone suggest a workable approach? Is it possible?
This is what I would like to be able to do...updating since I may have caused some confusion...
function executeSomeLogic() {
return new Promise((resolve,reject) => {
var err = doSomeWork();
if (!err) {
resolve("Finished without error");
} else {
// I cannot get this to work. reject does not return a value.
// ret is always undefined after this next line
let ret = reject("Return true if you want to roll back");
if (ret) {
doSomeInternalCleanup();
}
}
});
}
// this runs in completely different place/module/file and simply invokes executeSomeLogic function. Returning anything from catch does not do anything...
executeSomeLogic().then(result => {
console.log("all good");
}).catch(error => {
// Here, is client code I want to tell the calling code that I want
// to clean up ONLY if error message match what I am looking for.
// Problem is returning anything from catch is not doing anything to
// the code that called reject...
console.log("no good");
if (error == "Return true if you want to roll back"){
return true;
} else {
return false;
}
});