What does this code do?

I am trouble shooting some code and a little confused on what the following code does, especially at the end, the })) {…

// Check if the device has multiple cameras
if (navigator.mediaDevices.enumerateDevices().then(devices => {
  let cameras = devices.filter(device => device.kind === 'videoinput');
        console.log("cameras = ", cameras);
  return cameras.length > 1;
})) {

It tells you what it does in the comment on the first line.

If you’re asking how it does it, please be more specific about what is confusing you.

especially at the end, the })) {…

That is just JS. Brackets and parentheses need to be closed.

Perhaps it is sloppily written. I might reorganize it like this:

const hasMultipleCameras = navigator.mediaDevices.enumerateDevices()
  .then(devices => {
    let cameras = devices.filter(device => device.kind === 'videoinput');
    return cameras.length > 1;

if (hasMultipleCameras) {
1 Like

But I would expect that to be async so I’m not sure I’d expect that to work, anyway. Unless I am missing something.

Yeah, I think it kinda can be replaced with (await navigator.mediaDevices.enumerateDevices()).filter(device => device.kind === 'videoinput').length. You can also put it in if statement and assign it to temporary variable, if you want to print cameras.