DenoDDeno
Powered by
linrongbin16L
Denoβ€’2y ago
linrongbin16

Is it possible to embed `deno_core` with a self-managed event loop?

And the 2nd question is: does the API
deno_core::JsRuntime::run_event_loop
deno_core::JsRuntime::run_event_loop
only run 1 tick? or it try to wait everything inside its internal queue to complete?

Because after reading some tutorials and source code of
deno_core
deno_core
, I found there's already some queues inside
deno_core
deno_core
, I guess they're for the
queueMicrotask
queueMicrotask
and built-in
async
async
/
await
await
functions.

For example, if we have below sample code:

async function readFile(filename) {
   // Read a file in async mode
}

async function sleepFor(seconds) {
  // Sleep for seconds
}

const content = await readFile("README.md");
console.log(`We have read: ${content}`);
await sleepFor(10);
console.log(`We have sleep for 10s`);
async function readFile(filename) {
   // Read a file in async mode
}

async function sleepFor(seconds) {
  // Sleep for seconds
}

const content = await readFile("README.md");
console.log(`We have read: ${content}`);
await sleepFor(10);
console.log(`We have sleep for 10s`);


Once the
deno_core::JsRuntime
deno_core::JsRuntime
execute it, I guess the 1st
console.log
console.log
will not be printed, instead it's been put in the internal queue and wait for the
async readFile
async readFile
function done. Correct?

Then we call the
js_runtime.run_event_loop().await
js_runtime.run_event_loop().await
, once it's completed, is the only 1st
console.log
console.log
been printed? Or both two
console.log
console.log
are been printed? i.e. the
run_event_loop
run_event_loop
API runs for only 1 tick, or wait for all the tasks done?

If it only runs for 1 tick, then I can embed it into my command line TUI application. Because the TUI application is using tokio runtime with the crossterm library. I want to use
tokio::select!
tokio::select!
on both the crossterm's
EventStream
EventStream
(it receives terminal keyboard/mouse events, such as key pressed, mouse clicked, etc) and the
deno_core::JsRuntime::run_event_loop
deno_core::JsRuntime::run_event_loop
. Only 1 tick will never block the terminal events.

If it waits for all async tasks done, then it means it cannot work along with the tokio runtime managed by my TUI application. Because it will block the terminal.
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Is it possible to use Yarn with Deno?
wuX4anWwuX4an / help
2y ago
Deno event loop and gui application
ExidexEExidex / help
3y ago
Embed Deno into Rust App: How to call other method with poll_event_loop keep going
nuclearwwcNnuclearwwc / help
7mo ago