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

标签 javascript async-await azure-blob-storage

这是我引发异常的代码。无法弄清楚为什么它说“意外的保留字”。 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确实需要它

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

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

关于javascript - for await 给出 SyntaxError : Unexpected reserved word inside a async function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59184155/

相关文章:

javascript - 在 Angular 中获取第一个有条件的元素

javascript - Jquery ajax 在重新加载之前清除

Javascript 重复追加 Textarea 值问题

javascript - "The final argument passed to useEffect changed size between renders",但我不认为是这样?

javascript - 如何从 JSON 中提取和转换数据?

Python asyncio 等待线程

node.js - 在 try/catch block 中等待两个 promise 导致 "Unhandled promise rejection"

azure - 当文件到达 azure blob 存储时如何调用 POST Web api,并将文件发布到 api?

ASP.NET Web API Azure Blob 存储非结构化

azure - 在 azure blob 之间移动文件而不下载