The following situation:
function myFunction() {
return new Promise(function (resolve, reject) {
doSomething();
let myVariable = doSomethingElse();
let otherVariable = doOtherThings(myVariable);
return resolve(otherVariable);
});
}
Now, I want myVariable not to initialized by a function call, but within a callback, or, rather, within a .then
of a promise that is returned by an asynchronous function.
function myFunction() {
return new Promise(function (resolve, reject) {
doSomething();
let myVariable;
asynchronousFunctionThatReturnsPromise().then(function(param) {
myVariable = doSomethingElse(param);
});
let otherVariable = doOtherThings(myVariable);
return resolve(otherVariable);
});
}
Ideally the outer function would wait until myVariable is assigned a value, until it executes doOtherThings(myVariable)
, but I guess that is not possible within javascript.
Unfortunately, I cannot simply put all the following code in the "callback" function, since the outer functions return relies on the result.
Is there a way I can handle this, ideally without having to change anything on the outer function (myFunction)?