Code is not executed in Processing, but in p5.js Web Editor

Well, since you put it that way, you win!

If it were only a matter of waiting long enough for the deviceList array to fill, then what’s your explanation for why the code using your method won’t run in Processing IDE (4.02b) with a Safari browser (Mac)? Error message below is for two connected cameras:

You may not have the hardware to test this yourself, but this is why I think there’s more to it than meets the eye. If you are unable to test it, I would appreciate someone else testing it for confirmation. Thanks.

preload() should be renamed preloadForCertainThings() IMO.

Processing IDE with Safari Browser Code to Test (Mac) - Two Cameras

var deviceList = [];

navigator.mediaDevices.enumerateDevices().then(getDevices);

const setup = function () {
 var constraints = {
  video: {
  deviceId: {
    exact: deviceList[0].id
    },
   }
 };
 var constraints1 = {
  video: {
  deviceId: {
   exact: deviceList[1].id
  },
  }
 };
 canvas = createCanvas(width, height);
 background(255);
 video = createCapture(constraints);
 video2 = createCapture(constraints1);
 console.log(deviceList);
};

const draw = function () {
};

function startP5() {
  globalThis.setup = setup; // place callback setup() into global context
  globalThis.draw  = draw;
  new p5();
}

function getDevices(devices) {
  for (let i = 0; i < devices.length; ++i) {
    let deviceInfo = devices[i];
    //Only get videodevices and push them into deviceList
    if (deviceInfo.kind == 'videoinput') {
      deviceList.push( {
      label: deviceInfo.label,
        id: deviceInfo.deviceId
      }
      );
         console.log("Device name :", devices[i].label);
         console.log("DeviceID :", devices[i].deviceId);
    }
  }
  startP5();
}