pub fn with<V, F>(f: F) -> impl for<'cx> TryIntoJs<'cx, Value = V>
Expand description
Wraps a closure that will be lazily evaluated when TryIntoJs::try_into_js
is
called.
Useful for executing arbitrary code on the main thread before returning from a
function exported with neon::export
.
Note: The return type is JsResult
. If you need to return a non-JavaScript type,
call TryIntoJs::try_into_js
.
See With
for example usage.
ยงExample
use std::time::Instant;
#[neon::export(task)]
fn sum(nums: Vec<f64>) -> impl for<'cx> TryIntoJs<'cx> {
let start = Instant::now();
let sum = nums.into_iter().sum::<f64>();
let log = format!("sum took {} ms", start.elapsed().as_millis());
extract::with(move |cx| -> NeonResult<_> {
cx.global::<JsObject>("console")?
.method(cx, "log")?
.arg(&log)?
.exec()?;
sum.try_into_js(cx)
})
}