title: How to wait until a for loop finishes using async await in javascript?
body: <p><strong>My use case.</strong></p>
  
  <ol>
  <li>I upload 5 images to the s3 server in the browser and get that images uploaded urls.</li>
  <li>Pass that urls to the back-end.</li>
  </ol>
  
  <p>This is my async function</p>
  
  <pre><code>try{
      await uploadImagesToS3(imagesArray);
  
      await saveUrlsInBackend();
  
  }catch(error){
  
  }
  </code></pre>
  
  <p>In my <strong>uploadImagesToS3</strong> function I'm trying to do something like this.</p>
  
  <pre><code>uploadImagesToS3(){
      resolve(FORLOOP)
  }
  </code></pre>
  
  <p>After for loop run 5 times I want to resolve it to the my main async function.</p>
  
  <p>This my real <strong>uploadImagesToS3</strong> function</p>
  
  <pre class="lang-js prettyprint-override"><code>onUpload(array, albumName) {
        return new Promise((resolve, reject) =&gt; {
          resolve(
            for (let index = 0; index &lt; array.length; index++) {
            var files = document.getElementById(array[index]).files;
            if (!files.length) {
              return alert("Please choose a file to upload first.");
            }
            var file = files[0];
            var fileName = file.name;
            var albumPhotosKey = encodeURIComponent(albumName) + "//";
  
            var photoKey = albumPhotosKey + fileName;
            self;
            s3.upload(
              {
                Key: photoKey,
                Body: file,
                ACL: "public-read"
              },
              (err, data) =&gt; {
                if (err) {
                  return alert(
                    "There was an error uploading your photo: ",
                    err.message
                  );
                }
                // alert("Successfully uploaded photo.");
                this.images[index].image_path = data.Location;
              }
            );
          }
          );
        });
      }
  </code></pre>
  
  <p>But it doesn't let me to use a for loop inside a resolve function.
  How could I achieve this async await mechanisms?</p>