0

I use Responsive voice (text to speech) and I have created some simple data in an array and I loop over it with a promise but when I'm putting the resolve in the method onend of Responsive voice it is not the end of voice and it calls the next data. Please see my code.

var data = [
 {'textTH': "สวัสดี ค่ะ", 'lang': "Thai Female", 'textEN': "Hello", 'langEN': "US English Female"},
 {'textTH': "สวัสดี ค่ะ", 'lang': "Thai Female", 'textEN': "Hello 2", 'langEN': "US English Female"}
];

var j = data.length;

function step2(i){
    return new Promise(function(resolve, reject){
    if (typeof data[i] == 'undefined'){
            reject("out of rang");
    }
    else{               
      var runEnglistSpeech = function () {
        responsiveVoice.speak(data[i].textEN, data[i].langEN, { rate: .7, onend: resolve()});
        console.log("done >>>> " + i);        
      };
        responsiveVoice.speak(data[i].textTH, data[i].lang, { rate: .9, onend: runEnglistSpeech });      
    }
  })
}

(async () => {
    for (let i = 0; i < j; i++) {
        await step2(i);
       }
})();

Reponsive cdn => https://code.responsivevoice.org/responsivevoice.js

ProgrammerPer
  • 1,125
  • 1
  • 11
  • 26
user3001046
  • 235
  • 1
  • 10
  • 28

2 Answers2

0

I'm try to timeout and it's work but i want to have any choice for this

setTimeout(function(){ resolve(); }, 6000);
user3001046
  • 235
  • 1
  • 10
  • 28
0

Your code is working. Change responsiveVoice.speak(data[i].textEN, data[i].langEN, { rate: .7, onend: resolve()}); to responsiveVoice.speak(data[i].textEN, data[i].langEN, { rate: .7, onend: resolve});

Murali
  • 41
  • 3