亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在承諾中解決承諾

在承諾中解決承諾

墨色風雨 2022-12-22 15:45:29
我遇到了一個問題,我有一個本機反應應用程序,我有一組操作來顯示視頻(我之前記錄了這些操作)。我對所有動作都有一個 for 循環,需要等待視頻達到某個時間戳才能觸發動作。為此,我使用一個開關來識別我所有的動作,并在內部等待一個承諾,以便在好的時間戳觸發動作。這是我的代碼:  isTimestampReached = (timestampToWait) => {    console.log(      `timestampToWait: ', ${timestampToWait}, currentTime: ${this.videoPlayerRef.controlButtonRef.getCurrentTime()}`,    );    return new Promise((resolve, reject) => {      if (        timestampToWait <          this.videoPlayerRef.controlButtonRef.getCurrentTime() + 0.05 &&        timestampToWait >          this.videoPlayerRef.controlButtonRef.getCurrentTime() - 0.05      ) {        console.log('timestamp Reached !');        resolve(true);      } else {        setTimeout(this.isTimestampReached, 100, timestampToWait);      }    });  };  previewRecording = async () => {    this.resetPlayer();    const {recordedActions} = this.state;    console.log('recordedActions: ', recordedActions);    for (const action of recordedActions) {      console.log('action', action);      switch (action.type) {        case 'play':          console.log('launch play');          // if (await this.isTimestampReached(action.timestamp)) {  // this is the same as the line under          await this.isTimestampReached(action.timestamp).then(() => {            this.videoPlayerRef.setState({              paused: false,            });            console.log('setPlay');          });          break;        case 'pause':          console.log('launch pause');          await this.isTimestampReached(action.timestamp).then(() => {            console.log('set Pause');            this.videoPlayerRef.setState({              paused: true,            });          }),      }    }  };和日志:我們可以看到我待在和里面for loop,switch因為我沒有得到console.log('pause outside loop');。但正如你所看到的,我也不明白console.log('set Pause');。所以這意味著我的承諾沒有解決。我認為問題在于在承諾中啟動承諾,因為對于第一種情況(播放)它直接起作用。但我不知道如何解決這個問題。預先感謝社區PS:我只放了 javascript 標簽,因為我認為這與 react 或 react-native 無關。
查看完整描述

2 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

這意味著我的 Promise 沒有解決。我認為問題在于在承諾中發起承諾。


的確。在 的執行者回調中new Promise,您只調用setTimeout但從不調用resolve()or reject()。100 毫秒后的isTimestampReached調用確實創建并返回了自己的承諾,原始的“外部”承諾從未得到解決。你可以通過做來解決這個問題


setTimeout(() => {

  resolve(this.isTimestampReached(timestampToWait);

}, 100);

但是使用async/await進行輪詢要容易得多:


async isTimestampReached(timestampToWait) {

  while (true) {

    const currentTime = this.videoPlayerRef.controlButtonRef.getCurrentTime();

    console.log(`timestampToWait: ${timestampToWait}, currentTime: ${currentTime}`);

    if (timestampToWait < currentTime + 0.05 &&

        timestampToWait > currentTime - 0.05) {

      console.log('timestamp Reached !');

      return true;

    }

    await new Promise(resolve => {

      setTimeout(resolve, 100);

    });

  }

}

(您可以重構它以使用更好的循環條件,但您明白了)。


查看完整回答
反對 回復 2022-12-22
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

  await this.isTimestampReached(action.timestamp).then(() => {

then 不會被執行,因為你等待


使用以下


const res =   await this.isTimestampReached(action.timestamp)


  this.videoPlayerRef.setState({

              paused: false,

            });

            console.log('setPlay');

或刪除等待


 this.isTimestampReached(action.timestamp).then(() => {

            this.videoPlayerRef.setState({

              paused: false,

            });

            console.log('setPlay');

          });


查看完整回答
反對 回復 2022-12-22
  • 2 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號