-
Notifications
You must be signed in to change notification settings - Fork 469
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
Comments
Async functions can be called at top level. |
I just wrote bindings to tinybench, which is a library for benchmarking https://github.com/cometkim/rescript-tinybench
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 |
@cometkim one could try to
|
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 |
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. |
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. |
to support top-level await
The text was updated successfully, but these errors were encountered: