title: How to make Chrome Downloads API wait until a download has ended?
body: <p>I'm having a problem while trying to make an extension. What the extension does is pretty simple, I have a URL with a ser of images, i get the src of all the images, and then use the Downloads API of chrome to download them on a folder. I have it, and works great, but there is a problem, all the downloads start consecutively, which constantly makes some downloads to fail, so i tried to make Chrome wait until a download has completed to start another. </p>
  
  <p>First i tried to search if the Downloads API has a way to verify this, but, at least from what i've searched, i haven't find a method to get the DownloadItem, there is only two ways to do this, with search and the own download method, but both use a callback that seems to be asynchronous, then i tried to add a while before the download, and change it's condition with the one of those methods, but alway loops itself because while it's in the while loop, doesn't continue the callback of them, same with global methods like handleChanged(). So, how could i make Chrome wait until a download has ended before starting another without looping itself?</p>
  
  <p>This is the part of the code i use for the downloads</p>
  
  <pre><code>    for(let i=0; i&lt;images.length; i++) {
          // Download image
          while(currentDownload) {
              if (cont == 10000000) {
                  currentDownload = false;
              } else {
                  cont = cont + 1;
              }
          };
          cont = 0;
          currentDownload = true;
          var downloadUrl = images[i].split(" . ")[0];
          img = images[i].split(" . ")[1];
          console.log(name+"/"+img);
          var downloading = chrome.downloads.download({
              url: downloadUrl,
              filename: name+"/"+img,
              conflictAction: 'uniquify'
          });
  
      }
  </code></pre>
  
  <p>I put a counter on the while because the looping of my other tests were making my browser to crash, but it's better if there is a way to check until the download has ended before starting the next one. This is the listener i'm using to make the changes, just to be clear, i tried to put the search method on the prev code, even inside the while, and it just didn't worked. currentDownload is a global var.</p>
  
  <pre><code>function handleChanged(delta) {
      //if (urlRegexD.test(pest)) {
  
          if (delta.state &amp;&amp; delta.state.current === "complete") {
              console.log(`Download ${delta.id} has completed.`);
              currentDownload = false;
  
          }
      //}
  }
  
  chrome.downloads.onChanged.addListener(handleChanged)
  </code></pre>