varad
varad

Reputation: 8039

javascript use await in my function call

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 ?

Upvotes: 1

Views: 359

Answers (2)

coco puffs
coco puffs

Reputation: 1090

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

Upvotes: 1

David Pine
David Pine

Reputation: 24545

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;
    }

Upvotes: 2

Related Questions