Javascript 获取所有可用话筒-Map不是一项功能

Javascript 获取所有可用话筒-Map不是一项功能,javascript,Javascript,我有以下代码用可用的麦克风填充选择 const audioInputSelect = document.querySelector('select#audioSource'); // Updates the select element with the provided set of cameras function updateMicrophoneList(microphones) { console.log(microphones); audioInputSelect.i

我有以下代码用可用的麦克风填充
选择

const audioInputSelect = document.querySelector('select#audioSource');

// Updates the select element with the provided set of cameras
function updateMicrophoneList(microphones) {
    console.log(microphones);
    audioInputSelect.innerHTML = '';
    microphones.map(microphone => {
        const microphoneOption = document.createElement('option');
        microphoneOption.label = microphone.label;
        microphoneOption.value = microphone.deviceId;
    }).forEach(microphoneOption => audioInputSelect.add(microphoneOption));
}


// Fetch an array of devices of a certain type
async function getConnectedDevices(type) {
    const devices = await navigator.mediaDevices.enumerateDevices();
    return devices.filter(device => device.kind === type)
}


// Get the initial set of cameras connected
const microphonesList = getConnectedDevices('audioinput');
updateMicrophoneList(microphonesList);

// Listen for changes to media devices and update the list accordingly
navigator.mediaDevices.addEventListener('devicechange', event => {
    const newMicrophoneList = getConnectedDevices('audioinput');
    updateMicrophoneList(newMicrophoneList);
});
我发现了错误

VM1759 audio_devices.js:7 Uncaught TypeError: microphones.map is not a function
    at updateMicrophoneList (VM1759 audio_devices.js:7)
    at VM1759 audio_devices.js:24

为什么
map
在这里不起作用?

getConnectedDevices
是一个异步函数,意味着它返回一个承诺而不是数组。您可以使用
.then
功能在承诺履行时更新列表

getConnectedDevices('audioinput').then(updateMicrophoneList);

getConnectedDevices
是一个异步函数,意味着它返回一个承诺而不是数组。您可以使用
.then
功能在承诺履行时更新列表

getConnectedDevices('audioinput').then(updateMicrophoneList);

getConnectedDevices
是一个
async
函数,因此它将返回一个承诺。因此,
话筒
将是一个承诺,而不是一个数组
异步
函数总是返回一个
承诺
,因此要么
等待
调用
getConnectedDevices
函数,然后调用
updateMicrophoneList
或链a
然后()
method to
getConnectedDevices
函数调用,并从
then()
method
getConnectedDevices
的回调函数调用
updateMiprophoneList
函数,是一个
async
函数,因此它将返回一个承诺。因此,
话筒
将是一个承诺,而不是一个数组
异步
函数总是返回一个
承诺
,因此要么
等待
调用
getConnectedDevices
函数,然后调用
updateMicrophoneList
或链a
然后()
method to
getConnectedDevices
函数调用,并从
then()
method的回调函数调用
updateMicrophoneList
函数谢谢,刚刚开始使用JS,所以仍然习惯于承诺的工作方式。这就解决了这个问题,但现在我得到了“TypeError:未能在“HTMLSelectElement”上执行“add”:提供的值不是类型“(HTMLOptionElement或HTMLOptGroupElement)”,谢谢,刚刚开始使用JS,所以仍然习惯于承诺的工作方式。这就解决了这个问题,但现在我得到了“TypeError:未能在“HTMLSelectElement”上执行“add”:提供的值不是类型“(HTMLOptionElement或HTMLOptGroupElement)”