4

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

3
  • Yes there is a way: show us your code. Commented Sep 20, 2017 at 10:13
  • Updated the question with code. Thanks Commented Sep 20, 2017 at 10:19
  • Possible duplicate of 'await Unexpected identifier' on Node.js 7.5 Commented Nov 15, 2017 at 20:45

2 Answers 2

5

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!");
Sign up to request clarification or add additional context in comments.

7 Comments

Hi, I tried this now and updated question. I'm still getting out of order console.log statements. :( @TGrif
Yes, this is the expected result order.
Is there a way i can block execution of console.log("whoops Step 3!"); until method A completes?
@user461112 an async function returns a promise, so you can treat it like one: methodA(options).then(() => console.log('Step 3!'))
@robertklep thanks! that works. How would it work when you have two methods - similar methodA and methodB (both with async and await)
|
0
'use strict'

function methodA(options) {

    return new Promise(resolve => {
        setTimeout(() => {
            console.log(1)
            resolve(true);
        }, 2000);
    })
}

//Sync Declartion
async function test() {
    //Await declaration
    await methodA({});
    console.log(2);
}
test();

It seems there's is some syntax error in your code. Above code works in 8.5.0

Reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.