javascript - What will happen if I use asyncawait with the function that returns nothing? - Stack Overflow

First of all, I am a beginner at javascript, especially in asyncawaitpromise.I want to use seekTo() m

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745394127a4625799.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信