等待回调导致程序退出



使用"提示"和"说"编写简单的 Node.js CLI 文本到语音转换应用程序。如何在显示新的提示行之前提示等待语音完成?

我将chat((移到了回调之外(请参阅末尾的注释代码(,但这会导致提示提前出现(正常行为(。

我希望提示在语音完成后返回,因为一旦 say.speak 回调运行,聊天((就会重新启动。

程序在say.speak完成后立即退出,不显示新的提示。

const prompt = require('prompt')
const say = require('say')
chat()
function chat() {
  prompt.get(['chat'], (error, result)=> {
    if(error) return console.log('prompt <ERROR>: '+error.message)
    if (result.chat === 'exit') {
        terminate_self()
    } else if (result.chat == '') {
      say.speak('No input received.', '', '', ()=> {
        chat() // program terminates once speech is complete
      })
    } else {
      // process input here
      say.speak(result.chat, '', '', ()=> {
        chat() // program terminates once speech is complete
      })
    }
    // chat() // program stays alive, BUT prompt returns before speech is completed
  })
}
current result:
user@homeserver:~/Nodejs/ttschat$ node bin.js
chat |  hey
user@homeserver:~/Nodejs/ttschat$
desired result:
user@homeserver:~/Nodejs/ttschat$ node bin.js
chat |  hey
chat |  hey
chat |  exit
user@homeserver:~/Nodejs/ttschat$

未确定的本地问题。

我删除了项目文件夹,创建了相同的文件夹,npm 安装了 say 和提示符,并粘贴了与以前完全相同的代码,它现在按预期工作。

非常感谢 dun32 确认问题不在代码本身中!

最新更新