繁体   English   中英

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

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

这是我抛出异常的代码。 无法弄清楚为什么它会说“意外的保留字”。 main() 函数是一种异步类型。 它不会抱怨上面的代码行。

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();

您收到此错误是因为您的 Node 版本低于 10.0 并且不支持for await...of 作为旁注, for await在这里没有任何影响,可以替换为仅for原来 api 确实需要它


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

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

有一种解决方法:

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