Skip to content

Instantly share code, notes, and snippets.

@Dubiy
Last active July 5, 2023 16:20
Show Gist options
  • Save Dubiy/a761662f95341bc530574dec9f29e02a to your computer and use it in GitHub Desktop.
Save Dubiy/a761662f95341bc530574dec9f29e02a to your computer and use it in GitHub Desktop.
using Jint;
using UnityEngine;
using System;
using System.Threading.Tasks;
public class RunJs : MonoBehaviour
{
private static Engine engine;
void Start()
{
engine = new Engine();
engine.SetValue("console",typeof(Debug));
engine.SetValue("setTimeout", new Action<Action<int>, int>(setTimeout));
engine.Execute(@"
function delayResolve(delay) {
return new Promise(function(resolve) {
setTimeout(function() {
console.log('onTimeout');
resolve('success');
}, delay);
// resolve('works, if not called from C# inoked function')
});
}
delayResolve(1000)
.then(res => {
console.log('resolved: ' + res);
})
.catch(res => {
console.log('rejected: ' + res);
})
;
");
void setTimeout(Action<int> callback, int delay)
{
Task.Delay(delay).ContinueWith(_ => callback(delay));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment