1

Lets say I have a functin like:

function some(){
    console.log("wow")
    return "some"
}

var some_test = await some()
console.log(some_test)

I know it works without await but I want to test await . Here it gives unexpected token error.

Why I am unable to use await here ?

7
  • async action I guess. I am new to javascript Commented Feb 4, 2016 at 8:14
  • 1
    It is an ES7 functionlity. I believe browser doesnot support this functionlity yet. Commented Feb 4, 2016 at 8:15
  • How can I make some call wait for untill its finished Commented Feb 4, 2016 at 8:16
  • 1
    More info on await is here jakearchibald.com/2014/es7-async-functions Commented Feb 4, 2016 at 8:17
  • Starting to look like c# Commented Feb 4, 2016 at 8:18

2 Answers 2

2

The keywords async and await are available in Node.js applications and not yet supported for client side JavaScript. In order to use these keywords, your Node.js application needs to be using the ES7 standards. Here's a nice introduction to the core concepts. Also, here is the official ECMAScript github repo that documents it.

The calling function must be marked as async and then another async method within that call can then be awaited using the await keyword, just like is done in C#. Examples of the syntax are found here.

async function chainAnimationsAsync(elem, animations) {
        let ret = null;
        try {
            for(const anim of animations) {
                ret = await anim(elem);
            }
        } catch(e) { /* ignore and keep going */ }
        return ret;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

It won't work on old phones and in Internet Explorer. caniuse.com/#feat=async-functions if your customers don't use either then you should be fine.
1

Async functions are supported in the latest versions of Chrome and FF already, and according to caniuse, in the upcoming Edge 15. The await keyword can only be used inside a function marked with async. A couple of good tutorials on the subject:

Async Function Documentation from Mozilla

Introduction to Async Functions

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.