Getting error when try to make visible a layer

const app = window.require("photoshop").app;
 var doc = app.activeDocument;
  for (var i = 0; i < doc.layers.length; i++) {
    doc.activeLayer = doc.layers[i];
     doc.layers[i].visible = true; ****
...
}

I am getting error line doc.layers[i].visible = true;

In debugger it says

Uncaught Error: Event: select may modify the state of Photoshop. Such events are only allowed from inside a modal scope

What is the right way to make a layer visible? Actually I’ve develope my extension with CEP, and try to migrate to UXP. I notice that most of code are same about javascript. visible attribute is same as CEP. But I can’t see anywhere the right way.

Thanks.

As the error states, anything, that changes the state, can only be executed inside a modal state. Here’re related docs