How to make a repeat loop wait for a function to be executed,

Asked

Viewed 354 times

0

I have a program that downloads the file that is passed to him, follows the code

function Download(arquivo) {
  download("https://dumps.wikimedia.org/ptwiki/20190801/" + arquivo).then(
    data => {
      fs.writeFileSync(__dirname + "/downloads/" + arquivo, data);
      console.log("Download concluido, arquivo " + arquivo);
    }
  );
}

I have another list that has all the files that must be downloaded through this function ,it is contained inside an array . If I do

Download(lista[posicaoDoArquivoQueQueroBaixar]) ;

It makes the Download correctly,now if I do a go, or foreach to automatically download, it give an error by calling multiple Downloads at the same time,

lista.forEach(arquivo =>{
  Download(arquivo);
 });

That’s the mistake you make

(node:15351) UnhandledPromiseRejectionWarning: HTTPError: Response code 503 (Service Temporarily Unavailable)
    at EventEmitter.ee.on.res (/home/raellopes/Área de Trabalho/Trabalho/split xml document/node_modules/got/index.js:482:24)
    at EventEmitter.emit (events.js:198:13)
    at getResponse (/home/raellopes/Área de Trabalho/Trabalho/split xml document/node_modules/got/index.js:320:5)
    at Immediate.setImmediate (/home/raellopes/Área de Trabalho/Trabalho/split xml document/node_modules/got/index.js:147:6)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
(node:15351) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15351) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

Is there any way I could use for or foreach but the next loop would only run after the Download is completed?

(Note, the list has more than 60 files, to be exact 67, I tested running one at a time Download(list[x]) where X is the position from 0 to 67 and the download is done correctly, the error only give if I call more than 3x the Download at the same time)

1 answer

1


You can perform this processing using javascript async/await.

First you will adjust the function that performs the download to return a Promise, so you can use the await method to wait for the function to finish to proceed to the next item. See:

async function Download(arquivo) {
  const data = await download("https://dumps.wikimedia.org/ptwiki/20190801/" + arquivo)
  await fs.writeFileSync(__dirname + "/downloads/" + arquivo, data);
  return arquivo;
}

Now you need to use an async scope so you can run one at a time.

(async () => {
  for(const arquivo of lista){
    await Download(arquivo);
  }
})()

This will cause one download to run after another.

  • I’m in doubt again,Can you call me in Whats to give me a boost? (98) 98307-9191

Browser other questions tagged

You are not signed in. Login or sign up in order to post.