Function deno_core::json_op_async[][src]

pub fn json_op_async<F, V, R, RV>(op_fn: F) -> Box<OpFn> where
    F: Fn(Rc<RefCell<OpState>>, V, BufVec) -> R + 'static,
    V: DeserializeOwned,
    R: Future<Output = Result<RV, AnyError>> + 'static,
    RV: Serialize

Creates an op that passes data asynchronously using JSON.

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::json_op_async(Self::hello_op));

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

Deno.core.ops();
let future = Deno.core.jsonOpAsync("function_name", args);

The Deno.core.ops() statement is needed once before any op calls, for initialization. A more complete example is available in the examples directory.