Enforcing/validating the input of a function to not be undefined #2282
Answered
by
Brooooooklyn
ashley-hawkins
asked this question in
Q&A
|
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 |
Answered by
Brooooooklyn
Oct 1, 2024
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(())
} |
0 replies
Answer selected by
ashley-hawkins
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
Either: