首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >等待回调导致程序退出

等待回调导致程序退出
EN

Stack Overflow用户
提问于 2019-06-19 08:09:53
回答 1查看 750关注 0票数 1

使用“prompt”和“say”编写简单的Node.js文本到语音转换应用程序。如何使提示等待语音结束后再显示新的提示行?

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

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

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

代码语言:javascript
复制
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$
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56658514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档