Comments (4)
Well, i'll look into the code but my first guess is that the runtime is allready dropped when the async code runs.. just for the test, try to add a thread::sleep(Duration::from_secs(1)) after your last .await; in main()
from quickjs_es_runtime.
yeah, try something like this.... return promise from qjs and await it before exiting async main
use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use quickjs_runtime::jsutils::{JsError, Script};
use quickjs_runtime::values::JsValueFacade;
use serde_json::Value;
async fn cat_index() -> Result<Value, JsError> {
let value = serde_json::from_str("{\"abc\": \"i'm a obj made rusty\"}").unwrap();
println!("cat_index running");
Ok(value)
}
#[tokio::main]
async fn main() {
println!("Hello, world!");
let rt = QuickJsRuntimeBuilder::new().build();
rt.loop_realm(None, |_rt, realm| {
realm
.install_function(
&[],
"cat_elastic",
|_rt, realm, _this, _args| {
realm.create_resolving_promise_async(cat_index(), |realm, value| {
realm.serde_value_to_value_adapter(value)
})
},
0,
)
.unwrap();
realm
.install_function(
&[],
"log",
|_rt, realm, _this, args| {
println!("{:?}", args[0].to_string());
realm.create_undefined()
},
1,
)
.unwrap();
})
.await;
let promise_jsvf = rt
.eval(
None,
Script::new(
"promiseMeThis.js",
r#"
async function myCode(){
let res = await cat_elastic();
return res;
};
// eval statement, thus returning the promise
myCode()
"#,
),
)
.await
.expect("Script failed misserably");
if let JsValueFacade::JsPromise { cached_promise } = promise_jsvf {
// of boy oh boy we got a promise
let resolved_value = cached_promise
.get_promise_result()
.await
.expect("Promise timed out?");
match resolved_value {
Ok(prom_resolved) => {
println!(
"Promise resolved to {:?}",
prom_resolved
.to_json_string()
.await
.expect("no not so stringable?")
);
}
Err(_prom_rejected) => {
panic!("Promise was rejected");
}
}
}
}
from quickjs_es_runtime.
Ps just for clarity, this doesn't mean you always have to .await promises for them to run or anything, the problem in this case was just that the runtime would be destroyed before the async code ran...
from quickjs_es_runtime.
ok, this last piece of code solved my issue
if let JsValueFacade::JsPromise { cached_promise } = promise_jsvf {
// of boy oh boy we got a promise
let resolved_value = cached_promise
.get_promise_result()
.await
.expect("Promise timed out?");
match resolved_value {
Ok(prom_resolved) => {
println!(
"Promise resolved to {:?}",
prom_resolved
.to_json_string()
.await
.expect("no not so stringable?")
);
}
Err(_prom_rejected) => {
panic!("Promise was rejected");
}
}
}
basically yeah the code execution dropped before promise completion, i searched for a while a way to wait for the promise to end but i found the docs a bit lacking
from quickjs_es_runtime.
Related Issues (20)
- timeouts on invocations like call_function and eval HOT 2
- Ability to load pre-compiled modules HOT 11
- Option to disable features - console, setTimeout, setInterval HOT 9
- add console_formatter / console_logger to builder HOT 1
- Feat: function and struct to compute memory usage HOT 1
- Feat: TypedArrays HOT 1
- Panicked when running multiple promises concurrently HOT 2
- Document js_utils methods
- deprecate EsValueFacade HOT 1
- Segmentation fault: 11 & panic when using ScriptModuleLoader<QuickJsRealmAdapter> HOT 7
- fix MemoryUsage impl
- respect memory limit
- Main thread gets stuck randomly while awaiting for promise HOT 18
- thread panic occurred HOT 6
- Support for serde_json HOT 10
- Unexpected error logs even when the promise rejection is handled HOT 7
- Tokio Version in cargo.toml HOT 5
- Typescript support HOT 7
- fork quickjs HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quickjs_es_runtime.