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
1 Answer
Reset to default 5If a function (not async) does not explicitly return a value, then it returns
undefined
at the end of the functionYou can use such "no return" functions just fine in an async context - they just return a Promise that resolves to undefined
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
版权声明:本文标题:javascript - What will happen if I use asyncawait with the function that returns nothing? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745394126a2656735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论