function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mike O.Mike O. 

Dynamically selected value in a list

The selected value in a list is not set as expected. The code in the .js controller is:
setListLocations : function(cmp, response) { 
     var list = response.getReturnValue().Locations;
     var locations = []; 
     for (var i=0; i< list.length; i++) { 
         var loc = {value: list[i], label: list[i]}; locations.push(loc); 
      } 
        cmp.set("v.locations", locations); 
        cmp.set("v.selectedValue", response.getReturnValue().Location); 
},
From what I have been reading the value is not set as the list is not ready at that time. 

In the component I have:
<lightning:select name="mySelect" onchange="{!c.getValue }" label="To" aura:id="mySelect" value="{!v.selectedValue}" >
         <aura:iteration items="{!v.locations}" var="item">
            <option value="{!item.value}" selected="{!item.selected}">
                {!item.label}
            </option>
        </aura:iteration>
    </lightning:select>
From what I have been reading the value is not set as the list is not ready at that time.

I used a time delay between creating the list and setting the selected value in the list and it seems to do the trick.
window.setTimeout(
                $A.getCallback(function() {
                    cmp.set("v.selectedValue", response.getReturnValue().Location);  
                }), 75
            );
However, I'm not quite happy with this solution. 

Is there another more elegant way of fixing  this issue?