If I had two independent functions that are usually called separately like below:
function one() {
axios.post('XXXX', {
foo: foo,
})
.then((response) => {
//do something;
});
}
function two() {
axios.post('YYYY', {
foo: foo,
})
.then((response) => {
//do something;
});
}
but somewhere in my code, I had another function that called both of these but the execution was important in where I need two()
to run and finish executing before one()
does, I realize this below is unreliable.
function all() {
two();
one();
}
I want to ensure the times where I call these functions together elsewhere, I get them in the correct order and that the first finishes executing before the second is called.
Can I have rewrite the function two()
so it will return a Promise that just resolves()
with no argument inside of it so that it will trigger one()
like follows:
function two() {
return new Promise((resolve, reject) => {
axios.post('YYYY', {
foo: foo,
})
.then((response) => {
//do something;
})
.then((response) => {
resolve();
});
}
}