modernator
modernator

Reputation: 4427

Async Functions are not invoked sequentially

I have three async functions:

getAccounts = async () => {
    try {
        const result = await someAsyncJob();
        ...
    }
    catch(err) {
        ...
    }
}
getPages = async () => {
    try {
        const result = await someAsyncJob();
        ...
    }
    catch(err) {
        ...
    }
}
getDepositList = async () => {
    try {
        const result = await someAsyncJob();
        ...
    }
    catch(err) {
        ...
    }
}

Here, I have functions named "getAccounts", "getPages", "getDepositList". I need to invoke these functions sequentially, so I wrote the code like this:

getData = async () => {
    try {
        await getAccounts();
        await getPages();
        await getDepositList();
    }
    catch(err) {
        ...
    }
}

As from this article(https://medium.com/@peterchang_82818/asycn-await-bible-sequential-parallel-and-nest-4d1db7b8b95c), it must be work sequentially but when I ran this, they are invoke together and all of my logic was messed up.

How do I run those functions sequencially?

Upvotes: 0

Views: 230

Answers (1)

John G.
John G.

Reputation: 56

If the execution sees an await, it should only continue after the promise is resolved, or rejected, thus it should be sequentially. I hope you also do await getData()?

Upvotes: 3

Related Questions