Webpack: Use async/await in .vue file

Created on 2 May 2017  路  4Comments  路  Source: vuejs-templates/webpack

Trying a simple example:

async function asyncFun () {
  var value = 10
  return value
}
console.log(await asyncFun())

It always throw await is a reserved word. No matter the preset-env I change. Anyone could help on the correct settings for using async/await in this template ?

Thanks

Most helpful comment

Yeah, await cannot be used at the top level.

console.log(await asyncFun()) -> asyncFunc().then(v = console.log(v))

All 4 comments

Please refer to this issue https://github.com/babel/babel-eslint/issues/319.

As is explained by @hzoo,

you have to use await in an async function. The former isn't that syntax. You would be calling a function named async with a non-async arrow function.

Ok, thanks. Fixed with creating a whole async function around the await call.
It is better to read, but stills, I'm not sure if this is the right way to call:

async function asyncFun () {
  var value = 10
  return value
}
(async () => { console.log(await asyncFun()) })()

Looks a bit hard to read.
The code has been updated.

Yeah, await cannot be used at the top level.

console.log(await asyncFun()) -> asyncFunc().then(v = console.log(v))

Was this page helpful?
0 / 5 - 0 ratings

Related issues

exarus picture exarus  路  3Comments

paulgeisler picture paulgeisler  路  3Comments

brucejcw picture brucejcw  路  4Comments

dann95 picture dann95  路  3Comments

Splode picture Splode  路  3Comments