I have a promise whose abstract code is something like:
const myPromise = (input) => new Promise((resolve, reject) => {
//does something with input and provide some result
if (everything_is_ok) resolve(result);
else reject(error);
});
And this is the abstract flow of a procedure on my script:
let myVar;
//some code instructions...
myVar = something; //this comes as result of some code
if (condition){
//(once promises resolves) compute function does something with pr_output
//and provides another resulting output that gets stored on myVar for further computation
myPromise(takes myVar or some data as input here).then((pr_output)=>{myVar=compute(pr_output);});
}
//further operations with myVar follow here...
//AND, if condition was true, I want/need to be sure that the promise has resolved
//and the computation in its "then" instruction ended as well before going on...
So now the question is: (How) is it possible to go on without having to call a subsequent function? I mean I know I could simply do something like:
if (condition){
myPromise(takes myVar or some data as input here).then((pr_output)=>{myVar=compute(pr_output);
anotherProcedure(myVar); // <== THIS IS IT
});
} else anotherPocedure(myVar) // <== AND... THIS IS IT TOO
So I'd basically put every computation that comes after the condition check inside that anotherProcedure(myVar)
and call it (providing myVar as input):
- in the promise's
then
if condition was true - or in the
else
branch if condition was false
Is this the only way I can go or is it possible to avoid having to wrap the further computation inside that another procedure and call it that way? (If it is, please show me how to do it) Thank you