1
async function (){
    // await somthing
}

Uncaught SyntaxError: Unexpected token (

but i can define normal function like

function (){
    // ...
}()
3
  • 4
    but i can define normal function like Nope. Function statements require a function name. Did you mean to use an IIFE? Commented Dec 17, 2019 at 22:20
  • 2
    Agreed, the second snippet throws a syntax error just as the first one does. Commented Dec 17, 2019 at 22:20
  • 1
    bunch of useful comments about the syntax differences in here stackoverflow.com/questions/42964102/… Commented Dec 17, 2019 at 22:23

2 Answers 2

2

You need a function name for this syntax :

async function functionName(){
    // await somthing
}

You can use this syntax too if you don't want to name it :

async () => {
  //await something
}
Sign up to request clarification or add additional context in comments.

Comments

1

define an [anonymous] function and call immediately,parentheses needed

(async function (){
  return 1
}) () // and call it , Promise {<resolved>: 1}

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.