Ciao, ai fini di un applicativo che sfrutta la fotocamera come barcode/QR scanner, ho la seguente funzione che mi permette di capire se l'utente ha la cam accesa o spenta, ma non riesco a ritornare i valori sotto forma di response (ho bisogno del return di hasWebcam e isWebcamAlreadyCaptured).

Come fare?

codice:
checkCamAvaiable(){

        if(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices){
            navigator.enumerateDevices = function(callback) {
                navigator.mediaDevices.enumerateDevices().then(callback);
            };
        }


        var MediaDevices = [];
        var isHTTPs = location.protocol === 'https:';
        var canEnumerate = false;


        if (typeof MediaStreamTrack !== 'undefined' && 'getSources' in MediaStreamTrack) {
            canEnumerate = true;
        } else if (navigator.mediaDevices && !!navigator.mediaDevices.enumerateDevices) {
            canEnumerate = true;
        }


        var hasWebcam = false;
        var isWebcamAlreadyCaptured = false;




        function checkDeviceSupport(callback) {


            if (!canEnumerate) {
                return;
            }


            if (!navigator.enumerateDevices && window.MediaStreamTrack && window.MediaStreamTrack.getSources) {
                navigator.enumerateDevices = window.MediaStreamTrack.getSources.bind(window.MediaStreamTrack);
            }


            if (!navigator.enumerateDevices && navigator.enumerateDevices) {
                navigator.enumerateDevices = navigator.enumerateDevices.bind(navigator);
            }


            if (!navigator.enumerateDevices) {
                if (callback) {
                    callback();
                }
                return;
            }


            MediaDevices = [];
            navigator.enumerateDevices(function(devices) {


                devices.forEach(function(_device) {
                    var device = {};
                    for (var d in _device) {
                        device[d] = _device[d];
                    }


                    if (device.kind === 'audio') {
                        device.kind = 'audioinput';
                    }


                    if (device.kind === 'video') {
                        device.kind = 'videoinput';
                    }


                    var skip;
                    MediaDevices.forEach(function(d) {
                        if (d.id === device.id && d.kind === device.kind) {
                            skip = true;
                        }
                    });


                    if (skip) {
                        return;
                    }


                    if (!device.deviceId) {
                        device.deviceId = device.id;
                    }


                    if (!device.id) {
                        device.id = device.deviceId;
                    }


                    if (!device.label) {
                        device.label = 'Please invoke getUserMedia once.';
                        if (!isHTTPs) {
                            device.label = 'HTTPs is required to get label of this ' + device.kind + ' device.';
                        }
                    } else {
                        if (device.kind === 'videoinput' && !isWebcamAlreadyCaptured) {
                            isWebcamAlreadyCaptured = true;
                        }
                    }


                    if (device.kind === 'videoinput') {
                        hasWebcam = true;
                    }


                    MediaDevices.push(device);
                });


                if (callback) {
                    callback();
                }
            });
            
        }




        checkDeviceSupport(function() {
            console.log('hasWebCam: ', hasWebcam);
            console.log('isWebcamAlreadyCaptured: ', isWebcamAlreadyCaptured);
        });


    }