Function deno_core::op_async[][src]

pub fn op_async<F, A, B, R, RV>(op_fn: F) -> Box<OpFn> where
    F: Fn(Rc<RefCell<OpState>>, A, B) -> R + 'static,
    A: DeserializeOwned,
    B: DeserializeOwned,
    R: Future<Output = Result<RV, Error>> + 'static,
    RV: Serialize + 'static, 
Expand description

Creates an op that passes data asynchronously using JSON.

When this op is dispatched, the runtime doesn’t exit while processing it. Use op_async_unref instead if you want to make the runtime exit while processing it.

The provided function op_fn has the following parameters:

  • Rc<RefCell<OpState>: the op state, can be used to read/write resources in the runtime from an op.
  • V: the deserializable value that is passed to the Rust function.
  • BufVec: raw bytes passed along, usually not needed if the JSON value is used.

op_fn returns a future, whose output is a serializable value. This value will be asynchronously returned to JavaScript.

When registering an op like this…

let mut runtime = JsRuntime::new(...);
runtime.register_op("hello", deno_core::op_async(Self::hello_op));
runtime.sync_ops_cache();

…it can be invoked from JS using the provided name, for example:

let future = Deno.core.opAsync("hello", args);

runtime.sync_ops_cache() must be called after registering new ops A more complete example is available in the examples directory.