I have two functions: one plays audio, another one async function that should run after. I should run this sequence N times. But the problem is it runs in parallel. When the audio start playing second function also runs. The question is: how to run this functions consequently? The second function should run when the audio is finished.
const [song] = useState(typeof Audio !== 'undefined' && new Audio())
function playSong(songNum) {
song.src = songs[songNum]
song.play()
}
async function finalFunction() {
for (let i = 0; i < songs.length; i++) {
playSong(i)
await secondFunction().then((res) => {
console.log(res)
})
}
}
I tried to add promise, but it didn't help, it only runs the first function.
function playSong(songNum) {
return new Promise(() => {
song.src = songs[songNum]
song.play()
})
}
async function finalFunction() {
for (let i = 0; i < songs.length; i++) {
await playSong(i).then(() =>
secondFunction().then((res) => {
console.log(res)
}),
)
}
}