Why does my 'await' function not waiting for completion?

Hi,
I am trying to generate a function which will merge all visible layers.
I used alchemist and generates the function as shown below.
Before I use my “mergeVisible()” I must make sure that the current selected layer is an artLayer(pixes, smartobject), therefore I created another function to select any visible pixel or smartLayer.
The general structure for is this:

const { app, core, action } = require("photoshop");

async function myMergeVisible(){
  await selectVisibleArtLayer();
  await mergeVisible();
}

The problem is that the second function, mergeVisible starts before the first function, selectVisibleArtLayer() completes.
When I terminate the program after selectVisibleArtLayer(), the correct layer is selected.
When I let it run, it is not yet selected when mergeVisible() is reached.
It is selected, later on.
Why is that?
What should I do make sure the layer is selected prior to continue the program flow?

async function selectLayerById(layerId, isFirst = true) {
   await require("photoshop").core.executeAsModal(async (executionControl, descriptor) => {
      if (isFirst) {
         const result = await batchPlay(
            [
               {
                  _obj: "select",
                  _target: [
                     {
                        _ref: "layer",
                        _id: layerId
                     }
                  ],
                  makeVisible: false,
                  layerID: [
                     layerId
                  ],
                  _options: {
                     dialogOptions: "dontDisplay"
                  }
               }
            ], {
            synchronousExecution: false,
            modalBehavior: "execute"
         });
      }
      else {
         const result = await batchPlay(
            [
               {
                  _obj: "select",
                  _target: [
                     {
                        _ref: "layer",
                        _id: layerId
                     }
                  ],
                  makeVisible: false,
                  "selectionModifier": { "_enum": "selectionModifierType", "_value": "addToSelection" },
                  layerID: [
                     layerId
                  ],
                  _options: {
                     dialogOptions: "dontDisplay"
                  }
               }
            ], {
            synchronousExecution: false,
            modalBehavior: "execute"
         });
      }
      //
   }, {
      "commandName": "selectLayerById"
   });
}
async function mergeVisible() {
   //smartObject
   //pixel
   //make sure an art layer is selected.
   //this command will not work if a filter layer is selected
   await require("photoshop").core.executeAsModal(async (executionControl, descriptor) => {
      const batchPlay = require("photoshop").action.batchPlay;
      const result = await batchPlay(
         [
            {
               _obj: "mergeVisible",
               _options: {
                  dialogOptions: "dontDisplay"
               }
            }
         ], {
         synchronousExecution: false,
         modalBehavior: "execute"
      });
   }, {
      "commandName": "mergeVisible"
   });
}

You didn’t share what selectVisibleArtLayer() does

Sorry, here it is

async function selectVisibleArtLayer(){
  docRef = app.activeDocument;
  if (docRef.layers.length > 0) {
     for (var i = 0; i < docRef.layers.length; i++) {
         var theLayer = docRef.layers[i];
         if (theLayer.kind == "pixel" || theLayer.kind == "smartObject") {
             //select a visible layer - any layer
             if (theLayer.visible == true) {
                 await selectLayerById(theLayer._id);
                 break;
             }
         }
     }
  }
}

How about this instead?

const Layer = require('photoshop').app.Layer;
const lyr = new Layer(layerID, docID);
lyr.selected = true;

This one is sync. Make sure to wrap it into executeAsModal