Top-level async function returns immediately #602
Unanswered
Soundman32
asked this question in
Q&A
Replies: 1 comment
-
Hi @Soundman32,
When you call an async function, the return value is a promise. If you opt into ClearScript's task-promise interop, you can simply // assuming engine constructed with V8ScriptEngineFlags.EnableTaskPromiseConversion
await (Task)engine.Evaluate(@"
(async () => {
... <long running process>
})();
");
If you prefer to use the property bag as a signaling mechanism, you can do something like this to avoid polling: var success = new TaskCompletionSource<object>();
bag.PropertyChanged += (_, eventArgs) => {
if (eventArgs.PropertyName == "Success") {
success.SetResult(bag["Success"]);
}
};
engine.Evaluate(@"
(async () => {
... <long running process>
_bag.Success = true; // Signal that the script has completed successfully
})();
");
Console.WriteLine(await success.Task); Good luck! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to run a JS script that has async methods, so I have a top-level async method that runs everything (see below).
_bag is a C# PropertyBag that is added via AddHostObject
engine.Execute
returns immediately and I can see_bag.Completed
is set, but the script is still executing in the background (other C# methods and logging are seen AFTER the execute has returned).How do I make
Execute
wait until the top-level method has completed?EDIT: I've added this to the C# code, but it feels wrong
Beta Was this translation helpful? Give feedback.
All reactions