Daniel Stephens
Daniel Stephens

Reputation: 3259

await in for loop of TypeScript

I am new to Typescript and this is the first time for me dealing with async and await. I have an express endpoint which is not async. Inside I have a for-loop that seems to require await. What would be the common way in TypeScript to iterate over this loop?

app.get("/", function (req, res) {
    const resultIterator = client.query(
        'SELECT username FROM users;'
    );

    for await (const row of resultIterator) { <--- await illegal here
        // 'Hello world!'
    }
});

Upvotes: 1

Views: 395

Answers (1)

blackening
blackening

Reputation: 953

app.get("/", async function (req, res) {

Express endpoints don't care whether they are async or not. You can always make them async.

You need the async keyword for await to work.

Upvotes: 2

Related Questions