第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在承諾中解決承諾

在承諾中解決承諾

墨色風(fēng)雨 2022-12-22 15:45:29
我遇到了一個(gè)問題,我有一個(gè)本機(jī)反應(yīng)應(yīng)用程序,我有一組操作來顯示視頻(我之前記錄了這些操作)。我對(duì)所有動(dòng)作都有一個(gè) for 循環(huán),需要等待視頻達(dá)到某個(gè)時(shí)間戳才能觸發(fā)動(dòng)作。為此,我使用一個(gè)開關(guān)來識(shí)別我所有的動(dòng)作,并在內(nèi)部等待一個(gè)承諾,以便在好的時(shí)間戳觸發(fā)動(dòng)作。這是我的代碼:  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因?yàn)槲覜]有得到console.log('pause outside loop');。但正如你所看到的,我也不明白console.log('set Pause');。所以這意味著我的承諾沒有解決。我認(rèn)為問題在于在承諾中啟動(dòng)承諾,因?yàn)閷?duì)于第一種情況(播放)它直接起作用。但我不知道如何解決這個(gè)問題。預(yù)先感謝社區(qū)PS:我只放了 javascript 標(biāo)簽,因?yàn)槲艺J(rèn)為這與 react 或 react-native 無關(guān)。
查看完整描述

2 回答

?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

這意味著我的 Promise 沒有解決。我認(rèn)為問題在于在承諾中發(fā)起承諾。


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


setTimeout(() => {

  resolve(this.isTimestampReached(timestampToWait);

}, 100);

但是使用async/await進(jìn)行輪詢要容易得多:


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);

    });

  }

}

(您可以重構(gòu)它以使用更好的循環(huán)條件,但您明白了)。


查看完整回答
反對(duì) 回復(fù) 2022-12-22
?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

then 不會(huì)被執(zhí)行,因?yàn)槟愕却?/p>


使用以下


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');

          });


查看完整回答
反對(duì) 回復(fù) 2022-12-22
  • 2 回答
  • 0 關(guān)注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)