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

Tah*_*med 4 javascript azure-storage-blobs async-await

这是我抛出异常的代码。无法弄清楚为什么它会说“意外的保留字”。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();
Run Code Online (Sandbox Code Playgroud)

Max*_*Max 11

您收到此错误是因为您的 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();
}
Run Code Online (Sandbox Code Playgroud)