-
-
Notifications
You must be signed in to change notification settings - Fork 482
Closed
Description
How to get the result of JavaScript execution in the test_javascript async method?
async function search() {
try {
const uu ='https://example.com'
const res = await fetch_get(uu);
return res
} catch (error) {
console.log(error)
}
}
(async () => {
return await search();
})();fn fetch_get(
_this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> impl Future<Output = JsResult<JsValue>> {
let client = reqwest::Client::new();
let url = args
.get_or_undefined(0)
.to_string(context)
.unwrap()
.to_std_string()
.unwrap();
async move {
// tokio::time::sleep(Duration::from_secs(2)).await;
let response = client.get(url).send().await.unwrap();
let status = response.status();
println!("rust response status :{}",status);
Ok(JsValue::new(js_string!(status.to_string())))
}
}
#[tauri::command]
pub fn test_javascript(script: String) -> Result<String, String> {
let mut context = Context::default();
let console = Console::init(&mut context);
context
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
.expect("the console object shouldn't exist yet");
context
.register_global_builtin_callable(
js_string!("fetch_get"),
1,
NativeFunction::from_async_fn(fetch_get),
)
.expect("the sleep builtin shouldn't exist yet");
match context.eval(Source::from_bytes(&script)) {
Ok(res) => {
println!(
"rust eval:{}",
res.to_string(&mut context).unwrap().to_std_string_escaped()
);
Ok(res.to_string(&mut context).unwrap().to_std_string_escaped())
}
Err(e) => {
// Pretty print the error
eprintln!("Uncaught {e}");
Err(e.to_string())
}
}
}result is
rust response status :200 OK
rust eval:[object Promise]
should be. is rust eval:200 OK
Metadata
Metadata
Assignees
Labels
No labels