1

I was going through the WebRTC documentation and I found the following piece of code.

 const openMediaDevices = async (constraints) => {
    return await navigator.mediaDevices.getUserMedia(constraints);
}

try {
    const stream = openMediaDevices({'video':true,'audio':true});
    console.log('Got MediaStream:', stream);
} catch(error) {
    console.error('Error accessing media devices.', error);
}

Here, openMediaDevices() returns a promise while it have been used inside try{} block without any await. Will the program wait for openMediaDevices() to complete? If yes, how?

3
  • returning Promise and async are different. In fact, internally Javascript is probably using Promise to make things functional with async and await keywords Commented Oct 22, 2020 at 7:12
  • @canbax async and await are mostly syntactic sugar over promises to allow alternative syntax. They aren't different. Commented Oct 22, 2020 at 7:17
  • Duplicate Question, check the answer in the below link stackoverflow.com/a/77946819/2980414 Commented Feb 6, 2024 at 10:08

1 Answer 1

2

Will the program wait for openMediaDevices() to complete?

No.

Sign up to request clarification or add additional context in comments.

4 Comments

Then they have the wrong program? Please check webrtc.org/getting-started/media-devices#using-asyncawait.
In more detail the webrtc.org code is missing an await operatator between const stream = and the immediately following call to openMediaDevices (at least at October 2020).
Making the helper function to give navigator.mediaDevices.getUserMedia a shorter name async is also entirely pointless.
the tutorial on webrtc.org is wrong in this and other places...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.