Enforcing/validating the input of a function to not be undefined #2282
-
If I define a function like so: #[napi]
pub fn example(env: Env, callback: Function<(String), JsUndefined>) -> napi::Result<()> {
callback.call("hi".to_owned())?;
Ok(())
} example() can be called without any arguments, and that causes a failure with the message "invalid arg", I guess because it's not possible to call Edit: I've noticed that I can use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can use #[napi]
pub fn example(env: Env, maybe_callback: Either<Function<(String), ()>, Unknown>) -> napi::Result<()> {
match maybe_callback {
Either::A(callback) => {
callback.call("hi".to_owned())?;
}
Either::B(whatever) => {},
}
Ok(())
} |
Beta Was this translation helpful? Give feedback.
You can use
Either
: