2 回答

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)條件,但您明白了)。

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');
});
添加回答
舉報(bào)