title: javascript - wait for loop to finish first
body: <p>I want to finish first all results before going to another loops. How can I manage to achieve that? </p>
  
  <pre><code>function callback_Original(results, status) { 
      if (status === google.maps.places.PlacesServiceStatus.OK) { 
        for (var i = 0; i &lt; results.length; i++) { 
                  createMarker_Original(results[i]);
        }
      }
    }
  </code></pre>
  
  <p>It always gives few places sometimes.</p>
  
  <pre><code>function createMarker_Original(place) {
      var photos = place.photos;
      if (!photos) {
          return;
      }
  
      var placeLoc = place.geometry.location;
      var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location
      });
  
  
  
      google.maps.event.addListener(marker, 'click', function() { 
            infowindow.setContent('&lt;div style="float:left"&gt;&lt;img src="' + photos[0].getUrl({'maxWidth': 120, 'maxHeight': 120}) 
            + '"&gt;&lt;/div&gt;&lt;div style="float:right; padding: 10px;"&gt;&lt;b&gt;Name: &lt;/b&gt;'+  place.name +'&lt;br/&gt;'+
            '&lt;b&gt;Coordinates : &lt;/b&gt;'+ place.geometry.location +'&lt;br/&gt;'+
            '&lt;b&gt;Type: &lt;/b&gt;'+ type +'&lt;br/&gt;');
            infowindow.open(map, this);
      });
  }
  </code></pre>