Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for await gives SyntaxError: Unexpected reserved word inside a async function

Here is my code that throws the exception. Not able to figure out why it says 'Unexpected reserved word'. main() function is an async type. It isn't complaining for above line of code.

const { BlobServiceClient } = require("@azure/storage-blob");
async function main() {
    try {
        const blobServiceClient = await BlobServiceClient.fromConnectionString('some string');
        let i = 1;
        const result = await blobServiceClient.listContainers();
        for await (const container of result) {
            console.log(`Container ${i++}: ${container.name}`);
        }
    } catch (error) {
        console.log(error);
    }
}

main();
like image 622
Taher Ghulam Mohammed Avatar asked Dec 04 '19 20:12

Taher Ghulam Mohammed


People also ask

How do I use async await?

async and await Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

How do I use async await with Axios?

Axios Request With Async/AwaitYou start by specifying the caller function as async . Then use the await keyword with the function call. Due to the await keyword, the asynchronous function pauses until the promise is resolved.

What is an async function Javascript?

The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically. For instance, this function returns a resolved promise with the result of 1 ; let's test it: async function f() { return 1; } f(). then(alert); // 1.


1 Answers

Your are getting this error because your Node version is lower than 10.0 and doesn't support for await...of. As a side note, for await has no effect here and can be substituted for just for Turns out api does need it


added: from the docs: you can either use for await of if your runtime supports it, or iterate an iterable in an old-fashioned way

let containerItem = await result.next();
while (!containerItem.done) {
  console.log(`Container ${i++}: ${containerItem.value.name}`);
  containerItem = await iter.next();
}
like image 95
Max Avatar answered Oct 22 '22 19:10

Max