I'm getting a headache from trying to come up with a solution, and I hope that someone here can solve my problem.
I am trying to create a program that will do a loop for a number of audio files, using NodeJS:
- Access audio file from source.
- Send audio file to outside API for treatment (this part is already handled).
- Obtain the answer to the audio file treatment
- Display the answer
The API cannot work on 2 or more audios at the same time because it mixes them up and returns incorrect results, so the audio list has to be handled sequentially (so, it does steps 1-5 for audio_1, then steps 1-5 for audio_2, etcetera).
I can do the loop just fine, but due to how Promises work, what the program actually does is this:
- Do step 1 for audio_1.
- Get a Promise to do step 2 for audio_1.
- Do step 1 for audio_2.
- Get a Promise to do step 2 for audio_2...
- Once it gets all the Promises, it listens in until all the the Promises are resolved.
And this messes things up due to the above mentioned problem.
Here's the code I have right now:
async function myFunction(client){
  ...
  const fileC = database()
  const vars = {"variable":"my_variable"}
  const url = "my_url"
  let result
  await fileC.find({}).forEach(f=>fileList.push(f))
  for (const f of fileList){
    await https.get(f.file_location,function(res){
      let form = new FormData()
      Object.keys(vars).forEach(key=>form.append(key,vars[key]))
      form.append("file",res,f.file_name)
      axios.post(url,form,{headers:form.getHeaders(),maxBodyLength:Infinity}).then((response)=>{
        console.log(response.data.val)
      })
    })
  }
}
My only obstacle is ensuring that the for loop will not move to one cycle until the previous one has finished - once I get that, then my full program should work.
Any ideas?
EDIT: modified a bunch of things because I realized I could skip them.
EDIT 2: just to point out. The objective is that the for loop will not go to do one cycle until the previous one is finished. I tried to do this using a while loop to block the cycle, but it didn't work, either.
 
    