Rasterizing layers in UXP

I’m trying to rasterize layer by going through all the layers in the document,

however I’m getting an error message saying that rasterize is not a function although I saw it in Photoshop API
Below is my code

function rasterizeLayers() {
  const app = window.require("photoshop").app;
  const allLayers = app.activeDocument.layers
  for (var j = 0; j < allLayers.length; j++) {
    allLayers[j].rasterize();
  }
}

Or does it need to be in batchplay?