I'm hoping my title and simplistic code explains what I'm looking to do. I'm trying to call myFunction(), then let those promises resolve with Promise.all... then, and then execute code after myfunction();
myfunction() {
let x = promiseOneFunction();
let y = promiseTwoFunction();
Promise.all([x, y]).then(values => {
let valOne = = values[0];
let valTwo = values[1];
returnObj = {
fistVal: valOne,
destination: valTwo
}
console.log("return object within promise is ", returnObj);
return returnObj;
});
} // end myfunction
myfunction();
console.log("want to have this log after my function is finished");
Thanks!
The only (sensible) way to run code when a promise completes is to either:
then()
on the promiseawait
the promiseThis means you need the promise to be available outside myfunction
. Currently that function returns nothing. Change it so it returns the promise returned by Promise.all([x, y]).then(...)
(which you currently ignore).
return Promise.all([x, y]).then(...)
Then that promise will be available outside the function:
const the_promise = myfunction();
And you can call then
on it.
the_promise.then(() => {
console.log("want to have this log after my function is finished");
});