1

Hey guys I would like to know syntax to await my own constructed function.

async function func(){
const result = await ()=>{
return 2;
}
console.log(result);
}

This example results in [Function] I would like to get 2

I am not able to google it as I do not know how to name it even sorry :)

2
  • You need to create a function which returns a Promise. Then call that function in await and the result of the promise will be store in the variable. Commented Jan 26, 2018 at 13:28
  • I understand that, I do not want to create new function i want to use arrow function instead of regular function Commented Jan 26, 2018 at 13:31

1 Answer 1

2

Because result is the function. You need to somehow invoke that function and assign the return of the function into result.

What you're looking for is:

async function func(){
    const result = await (()=>{
        return 2;
    })();
    console.log(result);
}

Or you can embrace the fact that result is a function and just do:

async function func(){
    const result = await ()=>{
        return 2;
    };
    console.log(result());
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, Thanks :) How would you name it so I can change title to this question ?
How to invoke arrow function after await statement ? or Could be ?
You can either self invoke it right after the definition, like my first code piece, or you can assign it to a variable and invoke the variable, like my second code piece.

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.