Times and Times Promises¶
Timers¶
import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.interop.V8Host;
import com.caoccao.javet.interop.V8Runtime;
import com.caoccao.javet.javenode.JNEventLoop;
import com.caoccao.javet.javenode.enums.JNModuleType;
public class TutorialTimersTimeout {
public static void main(String[] args) throws JavetException, InterruptedException {
try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime();
JNEventLoop eventLoop = new JNEventLoop(v8Runtime)) {
eventLoop.loadStaticModules(JNModuleType.Console, JNModuleType.Timers);
v8Runtime.getExecutor("const a = [];\n" +
"setTimeout(() => a.push('Hello Javenode'), 10);").executeVoid();
eventLoop.await();
v8Runtime.getExecutor("console.log(a[0]);").executeVoid();
}
}
}
Timers Promises¶
import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.interop.V8Host;
import com.caoccao.javet.interop.V8Runtime;
import com.caoccao.javet.javenode.JNEventLoop;
import com.caoccao.javet.javenode.enums.JNModuleType;
public class TutorialTimersPromisesTimeout {
public static void main(String[] args) throws JavetException, InterruptedException {
try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime();
JNEventLoop eventLoop = new JNEventLoop(v8Runtime)) {
eventLoop.loadStaticModules(JNModuleType.Console);
eventLoop.registerDynamicModules(JNModuleType.TimersPromises);
v8Runtime.getExecutor(
"import { setTimeout } from 'timers/promises';\n" +
"const a = [];\n" +
"setTimeout(10, 'Hello Javenode')\n" +
" .then(result => a.push(result));\n" +
"globalThis.a = a;").setModule(true).executeVoid();
eventLoop.await();
v8Runtime.getExecutor("console.log(a[0]);").executeVoid();
}
}
}