admin管理员组

文章数量:1425789

First of all, I am a beginner at javascript, especially in async/await/promise.

I want to use seekTo() method in the video library(react-native-youtube) synchronously. In MDN guide, it said Await Expression need Promise or some value. Is it possible to use the seekTo() method even if it returns nothing?

initVideo = async () => {
  await this._youTubeRef && this._youTubeRef.seekTo(startTime);
  this.setState({
    isPlaying: true
  });
}

First of all, I am a beginner at javascript, especially in async/await/promise.

I want to use seekTo() method in the video library(react-native-youtube) synchronously. In MDN guide, it said Await Expression need Promise or some value. Is it possible to use the seekTo() method even if it returns nothing?

initVideo = async () => {
  await this._youTubeRef && this._youTubeRef.seekTo(startTime);
  this.setState({
    isPlaying: true
  });
}

If someone knows well about the react-native-youtube library, please advise me how to set endTime of the video to section playback.

Share Improve this question edited Jan 18, 2019 at 17:35 Thelouras 8801 gold badge11 silver badges30 bronze badges asked Jan 18, 2019 at 17:06 MookMook 2471 gold badge3 silver badges5 bronze badges 4
  • 2 Async functions essentially always return a promise even if when resolved there's nothing contained within the promise so awaiting for a promise should work regardless of what the promise resolves to. – apokryfos Commented Jan 18, 2019 at 17:07
  • Thanks for your ment @apokryfos ! – Mook Commented Jan 18, 2019 at 17:48
  • The documentation doesn't say that seekTo returns a promise. Why is await needed? – Estus Flask Commented Jan 18, 2019 at 18:12
  • @estus I just want to set a playtime section for the video. The seekTo() method execute some actions after called and it would spend a few seconds. Although I didn't mention on my question, I need to call setTimeout() after seekTo() for section playback. – Mook Commented Jan 19, 2019 at 7:47
Add a ment  | 

1 Answer 1

Reset to default 5
  1. If a function (not async) does not explicitly return a value, then it returns undefined at the end of the function

  2. You can use such "no return" functions just fine in an async context - they just return a Promise that resolves to undefined

  3. Although I haven't worked with the library you mention, always keep in mind that await x is an expression, not a statement. So, if .seekTo is async, what you want is:

&& await this._youTubeRef.seekTo(startTime);

本文标签: javascriptWhat will happen if I use asyncawait with the function that returns nothingStack Overflow