How is responsive voice js working with promise?

622 Views Asked by At

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

2

There are 2 best solutions below

0
Murali On BEST ANSWER

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});

0
user3001046 On

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

setTimeout(function(){ resolve(); }, 6000);