在nodejs中使用await/async时出现意外的标识符

use*_*112 4 javascript promise async-await

当我在 Node.js 中使用 async 或 wait 时,我收到意外的标识符。我使用的是节点版本 8.5.0。对此完全封锁。有没有什么办法解决这一问题?

async function methodA(options) {
    rp(options)
        .then(function (body) {            
            serviceClusterData = JSON.parse(body);         
            console.log("Step 2");
            console.log("Getting cluster details from zookeeper");
        })
        .catch(function (err) {
            console.log("Get failed!");

        });
}

await methodA(options);
console.log("Step 3!");
Run Code Online (Sandbox Code Playgroud)

在第一次回答后尝试了这个:

var serviceClusterData = "";
            console.log("Step 1!");

            ////////////////////

            async function methodA(options) {
                await rp(options)
                    .then(function (body) {
                        serviceClusterData = JSON.parse(body);
                        console.log("Step 2");
                        console.log("Getting cluster details from zookeeper");
                    })
                    .catch(function (err) {
                        console.log("Get failed!");

                    });
            }

            methodA(options);
            console.log("whoops Step 3!");
Run Code Online (Sandbox Code Playgroud)

仍然出现故障:( 步骤 1 步骤 3 步骤 2

TGr*_*rif 5

您不能在异步函数之外使用await 。

async function methodA(options) {
    await rp(options)
        .then(function (body) {            
            serviceClusterData = JSON.parse(body);         
            console.log("Step 2");
            console.log("Getting cluster details from zookeeper");
        })
        .catch(function (err) {
            console.log("Get failed!");

        });
}

methodA(options);
console.log("Step 3!");
Run Code Online (Sandbox Code Playgroud)