leon
leon

Reputation: 1512

for await fails inside of the async function

Using "for await" in async Azure function generates following error:

Exception: Worker was unable to load function HttpTrigger: 'SyntaxError: Unexpected reserved word' Stack: D:\home\site\wwwroot\HttpTrigger\index.js:19 for await (const container of blobServiceClient.listContainers()) { ^^^^^

SyntaxError: Unexpected reserved word

Note that await is used inside of the async function and it should work:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request!!!!');

    const {ManagedIdentityCredential } = require ("@azure/identity")
    const { BlobServiceClient } = require("@azure/storage-blob"); // Change to "@azure/storage-blob" in your package

    let cred = new ManagedIdentityCredential()
    let myStr = ""    

    const blobServiceClient = new BlobServiceClient(
        // When using AnonymousCredential, following url should include a valid SAS or support public access
        `https://leonbragtest.blob.core.windows.net`,
        cred
      );

    let i = 1;

    (async () => {
        for await (const container of blobServiceClient.listContainers()) {
        console.log(`Container ${i++}: ${container.name}`);
        myStr += `Container ${i++}: ${container.name} `
        }
    })()

    context.res = {
        status: 200,
        body: "Time is:" + new Date() + "<br/>" + myStr
    };

};

When inner async wrapper is removed, the same error is generated.

What am I doing wrong calling for await inside of the async function ?

Upvotes: 1

Views: 96

Answers (1)

leon
leon

Reputation: 1512

Solved. Azure func runtime does not support “for await” (as I was pointed out). Using iter worked.

Upvotes: 2

Related Questions