Marak / say.js

TTS (text to speech) for node.js. send text from node.js to your speakers.
MIT License
1.48k stars 214 forks source link

say.speak does not work in for loop #136

Open HakimMalikOttey opened 8 months ago

HakimMalikOttey commented 8 months ago

While I can load the entire string into say.speak and have it say what I need to say, my goal for this project is to split the string into a list, and to iteratively have say.speak shout out each string in the list. However, the speak function does not say the text when put in any loop, but works fine if its outside of a loop.

    const responseData = response.text.split(/\r?\n/);
    if(responseData.length == 0){
      say.speak("Sorry. I do not think I heard that right.");
    }
    else{
      var sayPromise = [];
      for(var i = 0; i < responseData.length;i++){
        const resolve = () => {
          return new Promise((resolve) => {
            say.speak(responseData[i],"Alex",1,(err)=>{
resolve();
});
          })};
        sayPromise.push(resolve);
      }
      for(const async of sayPromise){
       const result =  await async();
      }      }
vladcorn commented 5 months ago

avoid async as variable also don't use resolve variable rename there are variables