jsfiddle / jsfiddle-users

Discussions, issues and docs for JSFiddle.
https://jsfiddle.net
314 stars 114 forks source link

top level await #1674

Open jimmywarting opened 3 years ago

jimmywarting commented 3 years ago

Ever thought about creating a iife async function so that you could use top level await?

would be cool!

jimmywarting commented 2 years ago

still want to be able to just simply import scripts in a easier way. top level await would be awesome to have

KaKi87 commented 2 years ago

Hello, Could you please reopen this ? Thanks.

mattrossman commented 1 year ago

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#browser_compatibility

browser compatibility table for top level await showing green checks across the board

Top level await is supported in all modern browsers, could JSFiddle please be updated to remove the error indicator from the editor?

error in editor, missing semicolon, though no such error is present
oskarkrawczyk commented 6 months ago

The linter is now updated, will not throw errors on async/await features.

That said, the following code will still throw an error:

const gift = await loader.loadAsync(src)

I don't know what this syntax is, never saw it before, ESLint in VSCode is also throwing an error and implying:

Screenshot 2024-04-30 at 10 59 04

If anyone know a way to configure ESLint so it accepts this syntax, I'll happily take it.

mattrossman commented 6 months ago

@oskarkrawczyk That syntax is top level await, it's supported in all browsers since 2021 https://github.com/jsfiddle/jsfiddle-issues/issues/1674#issuecomment-1491281331. I'm confused what was fixed here, as this issue was requesting that syntax 😅

The particular screenshot I showed was using Three.js to load a glTF asset, but you can await any promise at top level

await Promise.resolve()

JSFiddle can execute this code, it just displays an error in the editor.

https://jsfiddle.net/mattrossman/78gbLzd5/1/

Note: the import from lodash is just so that JSFiddle treats this script as type="module", though it would be nice if we had a better way to trigger this.

Editor Console

In a fresh project VSCode doesn't have an issue:

ESLint accepts top level await when configured with ECMAScript 2022 or newer. (ESLint Playground).

However, I don't see an ESLint error in your screenshot—that looks like an error from VSCode's TypeScript language server given the ts(1308) tag. TypeScript accepts top-level await with "module" set to ES2022 or later. (TypeScript Playground)

oskarkrawczyk commented 6 months ago

Thank you for the details.

I see what the issue is - we have ESLint statically configured for type script not module, no matter the ECMA version.

It'll take more work to get make this dynamic. Reopening.

oskarkrawczyk commented 6 months ago

@mattrossman Out of curiosity, would an editor option like Treat JS as module without the need of forcing it with an import is something you'd be interested in?

mattrossman commented 6 months ago

Yes, an editor option to treat the script as a module would be great!