# no-await-in-loop

Disallow await inside of loops.

You may want to await a promise until it is fulfilled or rejected, inside of loops. In such cases, to take full advantage of concurrency, you should not await the promise in every iteration, otherwise your async operations will be executed serially. Generally it is recommended that you create all promises, then use Promise.all for them. This way your async operations will be performed concurrently.

# Incorrect Code Exapmles

async function foo(xs) {
    const results = [];
    for (const x of xs) {
        // iteration does not proceed until `bar(x)` completes
        results.push(await bar(x));
    }
    return baz(results);
}

# Correct Code Examples

async function foo(xs) {
    const results = [];
    for (const x of xs) {
        // push a promise to the array; it does not prevent the iteration
        results.push(bar(x));
    }
    // we wait for all the promises concurrently
    return baz(await Promise.all(results));
}
More incorrect examples
async function foo() {
    const res = [];
    for(var i = 1; i < 20; i++) {
        res.push(await i);
    }
}
async () => {
    while(true) {
        await i;
    }
}

Source (opens new window)

Last Updated: 11/18/2020, 9:36:33 PM