简体   繁体   English

for await 给出 SyntaxError: Unexpected reserved word inside a async function

[英]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. main() 函数是一种异步类型。 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();

Your are getting this error because your Node version is lower than 10.0 and doesn't support for await...of .您收到此错误是因为您的 Node 版本低于 10.0 并且不支持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作为旁注, for await在这里没有任何影响,可以替换为仅for原来 api 确实需要它


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补充:来自文档:如果您的运行时支持它,您可以使用for await of ,或者以老式的方式迭代一个可迭代对象

let containerItem = await result.next();
while (!containerItem.done) {
  console.log(`Container ${i++}: ${containerItem.value.name}`);
  containerItem = await iter.next();
}

There is an work around for this one:有一种解决方法:

const { BlobServiceClient } = require("@azure/storage-blob");

async function main() {
    try {
        const blobServiceClient = await BlobServiceClient.fromConnectionString('DefaultEndpointsProtocol=https;AccountName=devappsstorage;AccountKey=aw/FF01OIfnYmK6Bh+d4NIhvVBaDKn942O22YAMNycX/27qKVwYW+/Dma/C7pI+lvU0wpo/VBJ1jFd4yi3MMWw==;EndpointSuffix=core.windows.net');
        let i = 1;
        let iter = blobServiceClient.listContainers();
        let containerItem = await iter.next();
        while (!containerItem.done) {
            console.log(`Container ${i++}: ${containerItem.value.name}`);
            containerItem = await iter.next();
          }
    } catch (error) {
        console.log(error);
    }
}

main();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM