I have multiple asynchronous functions that I want to execute one after the other however the next function is executing before the previous one has finished.
async function x() {
  for (...) {
    console.log("awaiting" + i)
    function fooPromise() {
      return new Promise(function(resolve) {
        foo();
        resolve();
      });
    }
    await fooPromise();
    console.log("awaited" + i)
  }
}
async foo(){
  for(...){
     await sleep(1500);    
  }    
  console.log("finished");
}
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
The expected output should be:
awaiting1
finished
awaited1
awaiting2
finished
awaited2
awaiting3
finished
awaited3
but instead I'm getting:
awaiting1
awaited1
awaiting2
awaited2
awaiting3
awaited3
finished
finished
finished
I'm fairly new to using promises and async so any help would be appreciated.
 
    