From 443dc0ffa136a0108f1479846a9ad0a2e50fac1c Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 25 Nov 2023 18:47:51 +0200 Subject: [PATCH] feat: add await function to promise --- src/me/topchetoeu/jscript/lib/PromiseLib.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/me/topchetoeu/jscript/lib/PromiseLib.java b/src/me/topchetoeu/jscript/lib/PromiseLib.java index 3c93a40..3df19c0 100644 --- a/src/me/topchetoeu/jscript/lib/PromiseLib.java +++ b/src/me/topchetoeu/jscript/lib/PromiseLib.java @@ -347,4 +347,19 @@ import me.topchetoeu.jscript.interop.Native; public PromiseLib() { this(STATE_PENDING, null); } + + public static PromiseLib await(Context ctx, PromiseRunner runner) { + var res = new PromiseLib(); + + new Thread(() -> { + try { + res.fulfill(ctx, runner.run()); + } + catch (EngineException e) { + res.reject(ctx, e.value); + } + }, "Promisifier").start(); + + return res; + } }