Open Dubiy opened 1 year ago
Jint doesn't support multi-threading and that's what your Task.Delay is doing.
Is there some alternative solution how to resume js code execution after c#? As far I understrand it's quite popular problem (fetch, setTimeout, etc...)
I think this would need some investigation and hopefully a PR that would both include tests and a remedy 😉
For me, Promise
doesn't even resolve on JS side:
new Promise(function (resolve) {
log('Promise!')
resolve(null)
}).then(function () {
log('Resolved!')
})
Expected output:
Promise!
Resolved!
Actual output:
Promise!
Can confirm the behavior is still the same as of 3.0
You can use c# tasks, they are converted to promises and awaited correctly.
so looking at the first example here, this would work:
using Jint;
using System;
using System.Threading.Tasks;
var engine = new Engine();
engine.SetValue("log", new Action<object>(Console.WriteLine));
engine.SetValue("setTimeout", new Func<int, Task>(setTimeout));
engine.Evaluate(@"
setTimeout(10000)
.then(res => {
log('resolved');
})
.catch(res => {
log('rejected');
})
;
");
async Task setTimeout(int delay)
{
await Task.Delay(delay).ConfigureAwait(false);
}
Just your standard C# and JS async/await way of working :)
I prefer to write it like this:
var engine = new Engine();
engine.SetValue("log", new Action<object>(Console.WriteLine));
engine.SetValue("setTimeout", new Func<int, Task>(setTimeout));
engine.Evaluate(@"
(async () => {
await setTimeout(1000);
log('resolved');
})();
");
async Task setTimeout(int delay)
{
await Task.Delay(delay).ConfigureAwait(false);
}
Version used
v3.0.0-beta-2049
Describe the bug
Promise not resolving, if resolve() invoked from C# callback function.
To Reproduce
GIST: https://gist.github.com/Dubiy/a761662f95341bc530574dec9f29e02a
Or code:
Expected behavior
Actual behavior