Skip to content

Make top-level scope as async context #5925

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

Closed
cometkim opened this issue Jan 2, 2023 · 7 comments · Fixed by #5940
Closed

Make top-level scope as async context #5925

cometkim opened this issue Jan 2, 2023 · 7 comments · Fixed by #5940

Comments

@cometkim
Copy link
Member

cometkim commented Jan 2, 2023

to support top-level await

@cristianoc
Copy link
Collaborator

Async functions can be called at top level.
Not sure if there's a compelling use for await at toplevel.

@cometkim
Copy link
Member Author

cometkim commented Jan 2, 2023

I just wrote bindings to tinybench, which is a library for benchmarking

https://github.com/cometkim/rescript-tinybench

Not sure if there's a compelling use for await at toplevel.

Following example

let run = async () => {
  await bench.run(.)

  open Belt
  bench.tasks->Array.forEach(task => {
    switch task {
    | {name, result: Some(result)} => {
        Js.log(`Task name: ${name}`)
        Js.log(`Average Time (ps): ${Float.toString(result.mean *. 1000.)}`)
        Js.log(`Variance (ps): ${Float.toString(result.variance *. 1000.)}`)
        Js.log("")
      }

    | {name} => Js.log(`Error occured while running bench ${name}`)
    }
  })
}

run()->ignore

could be just

await bench.run(.)

bench.tasks->Array.forEach(task => {
  switch task {
  | {name, result: Some(result)} => {
      Js.log(`Task name: ${name}`)
      Js.log(`Average Time (ps): ${Float.toString(result.mean *. 1000.)}`)
      Js.log(`Variance (ps): ${Float.toString(result.variance *. 1000.)}`)
      Js.log("")
    }

  | {name} => Js.log(`Error occured while running bench ${name}`)
  }
})

I'm using top-level await in most of my server projects, and this makes a significant difference in the application execution context. for example this case

@cristianoc
Copy link
Collaborator

@cometkim one could try to

  1. turn off the check for where await is allowed, so it's allowed everywhere, and see that examples compile fine;
  2. then, turn the check back on and allow specifically top-level bindings presumably of the form let x = await ....

cristianoc added a commit that referenced this issue Jan 11, 2023
cristianoc added a commit that referenced this issue Jan 11, 2023
@cristianoc
Copy link
Collaborator

cristianoc commented Jan 11, 2023

@cometkim take a look at #5940, with some examples.
I believe supporting toplevel await means that await can appear anywhere unless it's under a non-async function.

@cometkim
Copy link
Member Author

cometkim commented Jan 11, 2023

Yep, that's correct. but top-level await is available only on module context, which means ESM, not CJS

Maybe it should be depend on module-specs in bsconfig.json

@cristianoc
Copy link
Collaborator

Yep, that's correct. but top-level await is available only on module context, which means ESM, not CJS

Maybe it should be depend on module-specs in bsconfig.json

Not sure we want to add extra complexity to the language for this. It should be easy enough for the user to figure out that toplevel await does not work in CJS.

@zth
Copy link
Collaborator

zth commented Jan 12, 2023

I agree - not worth the extra complexity. Accidentally using it in cjs will crash early, so it should be easy to discover. Most bundlers, if you use one of those, will probably reject it too I guess.

cristianoc added a commit that referenced this issue Jan 12, 2023
cristianoc added a commit that referenced this issue Mar 9, 2023
cristianoc added a commit that referenced this issue Mar 9, 2023
cristianoc added a commit that referenced this issue Mar 9, 2023
cristianoc added a commit that referenced this issue Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants