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?
Promiseandasyncare different. In fact, internally Javascript is probably usingPromiseto make things functional withasyncandawaitkeywordsasyncandawaitare mostly syntactic sugar over promises to allow alternative syntax. They aren't different.