1

DEVELOPER LEVEL -
I've recently started to code. I'm working on this NodeJs Automation program, that uses Puppeteer.

THE PROJECT -
1.) I have a list of URLs from a Video-Platform WebSite.

> The Script goes to these URLs
> Saves the Data like Title, Tags, Actors etc
> Downloads the Video, Saves it locally.

.. >>Though this above 'Workflow' that I've given isn't important, but I still include it, don't know might help.

THE PROBLEM -
I'm using the for loop to go through each URL, and call my async function for the above ↑ work.

There is no problem with my async function - that gets all the data.

BUT the problem is - It doesn't wait to finish before moving to the next item in the loop.

If I set the for Loop's range to suppose 10 , then 10 async Functions start Together.

How do I set this code in a way, that my async functions run 1-by-1, along with the Loop.

CODE -



for (i = 0; i <= 10 + 1; i++) {



    (async () => {
        await automator(`${savedArray[i]}`).then(() => console.log("automator complete"))
    })()

    
}

Anupma
  • 53
  • 5
  • async itself means it will run asynchronously. try ```let res = await automator()``` and make the function that contains this for loop async. – Khushit Shah Mar 22 '21 at 09:53
  • You can't wait for something **asynchronous** to complete in **synchronous** code. I assume your `for` loop is not in an `async` function. That means the loop runs synchronously, and anything asynchronous that starts within it will not report completion until after the loop (and the job queue job that was running the loop) have finished. Your `async` function inside the loop waits for the `automator`'s work to finish, but it does so asynchronously; your `for` loop doesn't wait for your `async` function to finish (and can't, unless it can use `await`). One fix would be ... – T.J. Crowder Mar 22 '21 at 09:54
  • ... to put the entire code in an `async` function, then use ```await automator(`${savedArray[i]}`); console.log("automator complete");``` within the `for` loop: ```(async () => { for (let i = 0; i <= 11; ++i) { await automator(`${savedArray[i]}`); console.log("automator complete"); })().then(() => { console.log("All done"); }).catch(error => { /*...report/handle error...*/ });``` – T.J. Crowder Mar 22 '21 at 09:56
  • Note that I removed the use of `.then` inside the `async` part. There's no reason for `.then` directly within an `async` function. Use `await` instead, and put the code that should use the result after it. – T.J. Crowder Mar 22 '21 at 09:57
  • (FWIW, I go into promises and `async` functions in depth in Chapters 8 and 9 of my recent book *JavaScript: The New Toys*. Links in my profile if you're interested.) – T.J. Crowder Mar 22 '21 at 09:57
  • @T.J.Crowder, Thanks for your input Friend, But this doesn't work. Here's my code - ``(async () => { for (let i = 0; i <= 11; ++i) { await automator(`${savedArray[i]}`); console.log(`${savedArray[i]}`, "=", "automator complete"); }})().then(() => { console.log("All done"); }).catch(error => { /*error*/ });`` – Anupma Mar 22 '21 at 11:42
  • "It doesn't work" doesn't tell me anything. But that code will definitely "work" in that it will wait for a promise from `automator` to settle before continuing. If you're not seeing that, `automator` isn't returning a promise (or isn't returning one that waits to settle for whatever you're seeing it not wait for). – T.J. Crowder Mar 22 '21 at 11:46
  • @T.J.Crowder - How do I return a Promise in an ``async () => {}`` type of function . This is the code for this ↑ Automator Function - https://jsfiddle.net/0gevj7up/ – Anupma Mar 22 '21 at 11:53
  • I suggest you step back from what you're currently doing and read up on promises and `async` functions. An `async` function always returns a promise. To make that promise dependent on another promise you have inside it, use `await` or `return` (if you want to have your `async` function's promise settle based on that other promise). But I think your `automator` function isn't waiting for the things it's starting to finish (in particular, `downloadFile`), see [this question's answers](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises). – T.J. Crowder Mar 22 '21 at 12:02
  • @T.J.Crowder | Thanks a lot for your reply friend, I really appreciate ur time you invested here. As I said, I'm kind of a newbie in programming, and this concept of Async and Promises, seem really tough to work with. I'm not able to work with these, no matter how hard I try. This is my [code](https://jsfiddle.net/qyraz2j4/) to the `downloadFile` Function. -
    How do I change it so it waits for completion, before starting again, When I use it in another `async` Functions, like ↑ above. I wuld be really grateful if you can help me in this one.
    – Anupma Mar 23 '21 at 13:54

0 Answers0