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

support for asynchronicity in monaco-run #1505

Open
QuentinRoy opened this issue Apr 7, 2024 · 3 comments
Open

support for asynchronicity in monaco-run #1505

QuentinRoy opened this issue Apr 7, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@QuentinRoy
Copy link
Contributor

QuentinRoy commented Apr 7, 2024

Is your feature request related to a problem? Please describe.

When using async code in monaco run, later lines are not added. For example the following snippets only show 1 and 3 because the output is built synchronously.

console.log("1");
setTimeout(() => {
  console.log("2");
}, 2000);
console.log("3");

Describe the solution you'd like
I'd like the code output to be updated when the new log shows up.

Describe alternatives you've considered
Currently, I have successfully patched the ts-runner to return an element that mimics what <CodeRunner /> (from internals) outputs, but imperatively.

I am willing to make my patch a PR if the maintainers are interested. However I believe the best way to handle this would be to supports async generators as CodeRunnerOutputs. This way it would provide the functionality to all runners, and they won't have to manually do the little work (highlighting, etc) I am currently doing (since I am bypassing and replicating the usual rendering). I don't think it would be too hard to do, and am willing to give it a crack if the maintainers are interested (though expect delays).

@KermanX
Copy link
Member

KermanX commented Apr 8, 2024

The code is runned in a async context currently. You can use the await keyword on top level. However, setTimeout isn't working because the program finished before the callback, then the results aren't shown.

It is possible to override setTimeout in the running context, so that the program will only be regarded finished after all timeouts got triggered.

@QuentinRoy
Copy link
Contributor Author

It is probably a better approach then, because my solution currently has issues when exporting. But would it mean the program would only show the result once all setTimeout are done? I was currently thinking about overriding setTimeout in print mode and so have a fully different behavior in this case. But it starts to feel rather hacky.

@KermanX
Copy link
Member

KermanX commented Apr 8, 2024

But would it mean the program would only show the result once all setTimeout are done?

Yes. And we can improve this in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants