Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unexpected identifier when using await/async in nodejs

I'm getting unexpected identifier when i use async or await in nodejs. I'm on node version 8.5.0. Completely blocked on this. Is there anyway to fix this?

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!");

Tried this after first answer :

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!");

Still gets out of order :( Step 1 Step 3 Step 2

like image 205
user461112 Avatar asked Oct 29 '25 05:10

user461112


1 Answers

You can't use await outside of an async function.

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!");
like image 133
TGrif Avatar answered Oct 30 '25 23:10

TGrif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!