【问题标题】:Promise not getting resolved after setTimeOut承诺在 setTimeOut 之后没有得到解决
【发布时间】:2019-10-18 16:35:52
【问题描述】:

我正在使用 Cordova 的语音识别插件,在用户点击语音按钮 startRecognition 后触发,但如果在 10000 毫秒后如果用户没有说话,则应该触发 stopListening() 功能,除非用户说退出。

我正在使用 setTimeout 包装在一个承诺中,当该承诺得到解决时,我正在解决触发用于语音识别的科尔多瓦插件的承诺

除非用户说退出,否则此代码将持续运行语音识别。

  listenMsgAndReplyRepeat() {
    const self = this;
    this.listenMsg().then(res => {
      alert('got result ' + res);
      if (res === 'Exit') {
        self.exitFromApp();
      } else if (res === 'Success') {
        self.listenMsgAndReplyRepeat();
      } else if (res === 'Stopped') {
        alert('the speech recognition was stopped');
        self.botReply('Hello? Do you Want me to Exit?');
        self.listenMsgAndReplyRepeat();
      } else {
        self.botReply('Some error occured will detecting speech');
      }
    });
  }

这段代码是启动语音识别的代码

listenMsg() {
    const self = this;
    // Verify if recognition is available
    return new Promise(function(resolve, reject) {
      self
        .isRecognitionAvailable()
        .then(function(available) {
          if (available) {
            return window.plugins.speechRecognition.hasPermission();
          }
        })
        .then(function(hasPermission) {
          function startRecognition() {
            self.speakPopUp = true;
            return self
              .startRecognition({
                language: 'en-US',
                showPopup: false,
                matches: 2
                // showPartial: true
              })
              .then(function(result) {
                clearTimeout(self.stopListen);
                self.speakPopUp = false;
                if (result[0] === 'exit') {
                    resolve('Exit'); //working
                }
                self.botReply(result[0]).then(res => {
                  resolve('Success'); //working
                });
              })
              .catch(function(err) {
                reject('Error');
              });
          }
          if (!hasPermission) {
            self
              .requestPermission()
              .then(function() {
                self.stopSpeech().then(res => {
                  self.speakPopUp = false;
                  resolve('Stopped');  //this resolve is not working
                });
                startRecognition();
              })
              .catch(function(err) {
                console.error('Cannot get permission', err);
                reject('Error');
              });
          } else {
            self.stopSpeech().then(res => {
              self.speakPopUp = false;
              resolve('Stopped');  //this resolve is not working
            });
            startRecognition();
          }
        })
        .catch(function(err) {
          console.error(err);
          reject('Error');
        });
    });
  }

本地函数 startRecognition 内部的解析正在运行,但 if else 条件中的解析不起作用

这是stopRecognition 代码

  stopSpeech() {
    const self = this;
    return new Promise(function(resolve, reject) {
      self.stopListen = setTimeout(() => {
        self.stopListening().then(res => {
          clearTimeout(self.stopListen);
          resolve('stopped');
        });
      }, 10000);
    });
  }

setTimeOut 内部的解析正在起作用。

我将setTimeOut 分配给一个变量,因为如果用户说话我必须清除它,这样stopListening 就不会被触发。

【问题讨论】:

  • 您基本上是在一个 Promise 解析中包装了许多异步调用。由于您只处理一个解决方案(在您的新 Promise 中),因此您将在调用 resolve 时解决该承诺。 startRecognition 可能在 stopSpeech 之前完成,从而在语音 .then 完成之前解决您的主要承诺?
  • 作为一个提示,即使你得到了一个承诺,也总是使用 observables。将您的承诺转换为可观察的。这是个好习惯
  • @thsorens No.. 不总是。它会一直运行直到用户说话。如果用户在 10000 毫秒内没有说话,则在它之前触发 stopListening
  • 另外,如果我删除 setTimeOut 那么它正在工作

标签: javascript angular promise


【解决方案1】:

我能够通过使用 Promise.all() 来解决这个问题。我将超时包装在一个 Promise 中并将其传递到 Promise 数组中,然后我将 stopListening Promise 推送给它。

const promises = [];
promises.push(self.delay(10000));
promises.push(self.stopListening());
Promise.all(promises).then((res) => {
   self.speakPopUp = false;
   resolve('Stopped');
  });
  delay(t) {
    return new Promise(function(resolve) {
        setTimeout(resolve, t);
    });

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2019-04-13
    • 1970-01-01
    相关资源
    最近更新 更多