0

I have 2 functions with loops that I want to execute sequentially:

  • Function A executes

  • Within Function A there is a For loop triggering an external function

  • Once loop in Function A is finished doing its thing, then we start with Function B

I have tried with the following code but function B starts in parallel, doesn't wait for Function A to finish:

async function sendPosts() {
    console.log("start send posts")
    for (let post of posts) {
        ...
  
        await channel.send({
            ...
        }).then((message) => {
            externalFunction(message);
        })
        .catch(err => console.log(err));
    }
    console.log("finish send posts")    
}

async function sendComments() {
    console.log("start send comments")
    for (let comment of commentsData) {
        ... 
    }
    console.log("finish send comments")
}   

sendPosts().then(sendComments());
Eric Mitjans
  • 2,149
  • 5
  • 40
  • 69
  • 3
    `sendComments()` executes the function and assigns its return value as the resolve handler of `sendPosts()`. Just remove the `()` – Andreas Feb 02 '22 at 09:20
  • ooh right. Yeah I get you now! – Jamiec Feb 02 '22 at 09:33
  • 1
    `sendPosts` don't return `Promise` - then why you chainging like `Promise` `sendPosts().then(sendComments());` or I am blind?? – zbyso Feb 02 '22 at 09:43
  • @Andreas want to put this as answer and ill mark it as correct? – Eric Mitjans Feb 02 '22 at 09:44
  • Nope. I voted to close as typo (because I can never find the duplicate for this type of problem... :/). – Andreas Feb 02 '22 at 09:49
  • 3
    @zbyso `async` functions always return a promise. – Noam Feb 02 '22 at 10:22
  • 1
    @zbyso [Async function returning promise, instead of value](https://stackoverflow.com/q/51338277) | [async/await implicitly returns promise?](https://stackoverflow.com/q/35302431) | [Why is my asynchronous function returning Promise { } instead of a value?](https://stackoverflow.com/q/38884522) – VLAZ Feb 02 '22 at 12:53
  • `sendPosts().then(sendComments());` -> `sendPosts().then(() => sendComments());` or `sendPosts().then(sendComments);` see [What is the difference between a function call and function reference?](https://stackoverflow.com/q/15886272) | [addEventListener calls the function without me even asking it to](https://stackoverflow.com/q/16310423) | [In JavaScript, does it make a difference if I call a function with parentheses?](https://stackoverflow.com/q/3246928) – VLAZ Feb 02 '22 at 12:55
  • @VLAZ @Noam thanks, I don't know this: "If you don't explicitly return a promise, the value you return will automatically be wrapped in a promise.", I do manually `return new Promise...` every time :) – zbyso Feb 02 '22 at 13:55

1 Answers1

-1

For of loop is not awaited. you looking for something as following

    for await (const variable of iterable) 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of

Foreach loop has acting the same

Alantod17
  • 1
  • 3