1# Maybe (template)
2
3Class `Napi::Maybe<T>` represents a value that may be empty: every `Maybe` is
4either `Just` and contains a value, or `Nothing`, and does not. `Maybe` types
5are very common in node-addon-api code, as they represent that the function may
6throw a JavaScript exception and cause the program to be unable to evaluate any
7JavaScript code until the exception has been handled.
8
9Typically, the value wrapped in `Napi::Maybe<T>` is [`Napi::Value`] and its
10subclasses.
11
12## Methods
13
14### IsNothing
15
16```cpp
17template <typename T>
18bool Napi::Maybe::IsNothing() const;
19```
20
21Returns `true` if the `Maybe` is `Nothing` and does not contain a value, and
22`false` otherwise.
23
24### IsJust
25
26```cpp
27template <typename T>
28bool Napi::Maybe::IsJust() const;
29```
30
31Returns `true` if the `Maybe` is `Just` and contains a value, and `false`
32otherwise.
33
34### Check
35
36```cpp
37template <typename T>
38void Napi::Maybe::Check() const;
39```
40
41Short-hand for `Maybe::Unwrap()`, which doesn't return a value. Could be used
42where the actual value of the Maybe is not needed like `Object::Set`.
43If this Maybe is nothing (empty), node-addon-api will crash the
44process.
45
46### Unwrap
47
48```cpp
49template <typename T>
50T Napi::Maybe::Unwrap() const;
51```
52
53Return the value of type `T` contained in the Maybe. If this Maybe is
54nothing (empty), node-addon-api will crash the process.
55
56### UnwrapOr
57
58```cpp
59template <typename T>
60T Napi::Maybe::UnwrapOr(const T& default_value) const;
61```
62
63Return the value of type T contained in the Maybe, or use a default
64value if this Maybe is nothing (empty).
65
66### UnwrapTo
67
68```cpp
69template <typename T>
70bool Napi::Maybe::UnwrapTo(T* result) const;
71```
72
73Converts this Maybe to a value of type `T` in the `out`. If this Maybe is
74nothing (empty), `false` is returned and `out` is left untouched.
75
76[`Napi::Value`]: ./value.md
77