It appears that my use of await
is not behaving as I understood it to.
I thought that an async
function and the await
keyword could be used on a call that returns a promise (for instance navigator.mediaDevices.getUserMedia
) and it would pause the function execution (like a generator function) until the promise resolved and then it would continue on with the function.
Even though I am awaiting the call it's returning right away and my console logs are happening out of order. Specifically the console log starting with "reducer" appears in the console before the one starting with "getter", when it should be the other way around/is the opposite of how (I thought) the stack trace should run.
Can anyone tell me what's going on here?
Async helper function getter:
const getLocalStream = async () => {
try {
const localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
console.log('getter localStream', localStream);
console.log('about to return')
return localStream;
}
catch (err) {
console.error(err);
}
}
export default getLocalStream;
Reducer/store:
import getLocalStream from './getLocalStream';
const reducer = (state = {}, action) => {
switch (action.type) {
case 'SEND_CALL': {
const { id } = action;
const localStream = getLocalStream();
console.log('reducer localStream', localStream);
const call = state.peer.call(id, localStream);
return { ...state, call };
}
default:
return state;
}
};
Console:
reducer localStream Promise {<pending>}
Uncaught TypeError: Failed to execute 'addStream' on 'RTCPeerConnection': parameter 1 is not of type 'MediaStream'
getter localStream MediaStream {id: "B6Foz1jheGvnMqjiqC4PFoOvUVahF04zigzm", active: true, onaddtrack: null, onremovetrack: null, onactive: null, …}
about to return