18c339a94Sopenharmony_ci# Maybe (template)
28c339a94Sopenharmony_ci
38c339a94Sopenharmony_ciClass `Napi::Maybe<T>` represents a value that may be empty: every `Maybe` is
48c339a94Sopenharmony_cieither `Just` and contains a value, or `Nothing`, and does not. `Maybe` types
58c339a94Sopenharmony_ciare very common in node-addon-api code, as they represent that the function may
68c339a94Sopenharmony_cithrow a JavaScript exception and cause the program to be unable to evaluate any
78c339a94Sopenharmony_ciJavaScript code until the exception has been handled.
88c339a94Sopenharmony_ci
98c339a94Sopenharmony_ciTypically, the value wrapped in `Napi::Maybe<T>` is [`Napi::Value`] and its
108c339a94Sopenharmony_cisubclasses.
118c339a94Sopenharmony_ci
128c339a94Sopenharmony_ci## Methods
138c339a94Sopenharmony_ci
148c339a94Sopenharmony_ci### IsNothing
158c339a94Sopenharmony_ci
168c339a94Sopenharmony_ci```cpp
178c339a94Sopenharmony_citemplate <typename T>
188c339a94Sopenharmony_cibool Napi::Maybe::IsNothing() const;
198c339a94Sopenharmony_ci```
208c339a94Sopenharmony_ci
218c339a94Sopenharmony_ciReturns `true` if the `Maybe` is `Nothing` and does not contain a value, and
228c339a94Sopenharmony_ci`false` otherwise.
238c339a94Sopenharmony_ci
248c339a94Sopenharmony_ci### IsJust
258c339a94Sopenharmony_ci
268c339a94Sopenharmony_ci```cpp
278c339a94Sopenharmony_citemplate <typename T>
288c339a94Sopenharmony_cibool Napi::Maybe::IsJust() const;
298c339a94Sopenharmony_ci```
308c339a94Sopenharmony_ci
318c339a94Sopenharmony_ciReturns `true` if the `Maybe` is `Just` and contains a value, and `false`
328c339a94Sopenharmony_ciotherwise.
338c339a94Sopenharmony_ci
348c339a94Sopenharmony_ci### Check
358c339a94Sopenharmony_ci
368c339a94Sopenharmony_ci```cpp
378c339a94Sopenharmony_citemplate <typename T>
388c339a94Sopenharmony_civoid Napi::Maybe::Check() const;
398c339a94Sopenharmony_ci```
408c339a94Sopenharmony_ci
418c339a94Sopenharmony_ciShort-hand for `Maybe::Unwrap()`, which doesn't return a value. Could be used
428c339a94Sopenharmony_ciwhere the actual value of the Maybe is not needed like `Object::Set`.
438c339a94Sopenharmony_ciIf this Maybe is nothing (empty), node-addon-api will crash the
448c339a94Sopenharmony_ciprocess.
458c339a94Sopenharmony_ci
468c339a94Sopenharmony_ci### Unwrap
478c339a94Sopenharmony_ci
488c339a94Sopenharmony_ci```cpp
498c339a94Sopenharmony_citemplate <typename T>
508c339a94Sopenharmony_ciT Napi::Maybe::Unwrap() const;
518c339a94Sopenharmony_ci```
528c339a94Sopenharmony_ci
538c339a94Sopenharmony_ciReturn the value of type `T` contained in the Maybe. If this Maybe is
548c339a94Sopenharmony_cinothing (empty), node-addon-api will crash the process.
558c339a94Sopenharmony_ci
568c339a94Sopenharmony_ci### UnwrapOr
578c339a94Sopenharmony_ci
588c339a94Sopenharmony_ci```cpp
598c339a94Sopenharmony_citemplate <typename T>
608c339a94Sopenharmony_ciT Napi::Maybe::UnwrapOr(const T& default_value) const;
618c339a94Sopenharmony_ci```
628c339a94Sopenharmony_ci
638c339a94Sopenharmony_ciReturn the value of type T contained in the Maybe, or use a default
648c339a94Sopenharmony_civalue if this Maybe is nothing (empty).
658c339a94Sopenharmony_ci
668c339a94Sopenharmony_ci### UnwrapTo
678c339a94Sopenharmony_ci
688c339a94Sopenharmony_ci```cpp
698c339a94Sopenharmony_citemplate <typename T>
708c339a94Sopenharmony_cibool Napi::Maybe::UnwrapTo(T* result) const;
718c339a94Sopenharmony_ci```
728c339a94Sopenharmony_ci
738c339a94Sopenharmony_ciConverts this Maybe to a value of type `T` in the `out`. If this Maybe is
748c339a94Sopenharmony_cinothing (empty), `false` is returned and `out` is left untouched.
758c339a94Sopenharmony_ci
768c339a94Sopenharmony_ci[`Napi::Value`]: ./value.md
77