Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Top level async #345

Closed
michaelwclark opened this issue Mar 22, 2019 · 10 comments
Closed

Top level async #345

michaelwclark opened this issue Mar 22, 2019 · 10 comments

Comments

@michaelwclark
Copy link

Issue description or question

I'd love to see quokka handle async/await at top level. For quick prototyping this would be awesome!

Sample code

const test = async a => Promise.resolve(a)

console.log(test(4))
//Be awesome if we could.....
//console.log(await test(4)) 

//instead have to wrap async stuffs:
const getTest = async () => {
  const result = await test(4)
  console.log(result)
}

getTest().then()

Quokka.js Console Output

​​​​Quokka 'Untitled-3.js' (node: v10.5.0)​​​​

Promise {} ​​​​​at ​​​test(4)​​​ ​quokka.js:3:0​

4 ​​​​​at ​​​result​​​ ​quokka.js:10:2​
@smcenlly
Copy link
Member

Thanks for the feature request. We'll need to do some investigation to see if there are any side effects, but agree this would be a nice feature.

@ArtemGovorov
Copy link
Member

While not exactly the same as the originally requested feature, Quokka live comments (unlike console.log) support logging promise resolved values, so for your example you may do:

const test = async a => Promise.resolve(a)

test(4)//?

Screen Shot 2019-03-23 at 9 09 39 am

@ArtemGovorov
Copy link
Member

The feature is implemented in the latest extension version.

@timarandras
Copy link

@ArtemGovorov While the simple top-level await is working, this is not:

async function* asyncGen() {
  yield Promise.resolve(1);
}

for await (const ret of asyncGen()) {
  console.log(ret);
}
Failed to instrument quokka.js 
  3 | } 
  4 |  
> 5 | for await (const ret of asyncGen()) { 
    |     ^ SyntaxError: Unexpected token (5:4) 
  6 |   console.log(ret); 
  7 | }

Wrapping in an async funciton works tough:

async function f() {
  for await (const ret of asyncGen()) {
    console.log(ret);
  }
}

f()

What can we do?

@smcenlly
Copy link
Member

smcenlly commented Aug 7, 2019

@timarandras - thanks for reporting your problem. We've updated our instrumentation code to handle this case. Please update to the latest version of Quokka for your editor to get this fix.

@timarandras
Copy link

Works! Thank you.

@thunderkid
Copy link

This top-level await feature seems to work only in js files, and not in typescript files, where I still get the old await is only allowed inside an async function error. Is that correct? Any plans to allow in typescript too?

@smcenlly
Copy link
Member

smcenlly commented Nov 9, 2019

Unfortunately we can't support top-level await for TypeScript until it is supported by the TypeScript compiler. You may subscribe to the TypeScript issue if you are interested in updates.

@SwiftWinds
Copy link

Top-level await was shipped in TypeScript 3.8. However, it still doesn't work in Quokka:

image

I'm using Webstorm 2021.2.3 w/ Quokka v1.0.317

@ArtemGovorov
Copy link
Member

@SwiftWinds In order to make it work in Typescript, you need to configure your Quokka to use native ESM, so that your Quokka file runs as a module (as the error message suggests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants