node-addon-api 8.4.0 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,7 +19,7 @@ and exception handling semantics with low overhead.
19
19
  API references are available in the [doc](doc/README.md) directory.
20
20
 
21
21
  <!-- x-release-please-start-version -->
22
- ## Current version: 8.4.0
22
+ ## Current version: 8.5.0
23
23
  <!-- x-release-please-end -->
24
24
 
25
25
  (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog)
package/napi-inl.h CHANGED
@@ -2811,8 +2811,95 @@ inline void Promise::CheckCast(napi_env env, napi_value value) {
2811
2811
  NAPI_CHECK(result, "Promise::CheckCast", "value is not promise");
2812
2812
  }
2813
2813
 
2814
+ inline Promise::Promise() : Object() {}
2815
+
2814
2816
  inline Promise::Promise(napi_env env, napi_value value) : Object(env, value) {}
2815
2817
 
2818
+ inline MaybeOrValue<Promise> Promise::Then(napi_value onFulfilled) const {
2819
+ EscapableHandleScope scope(_env);
2820
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
2821
+ Value thenMethod;
2822
+ if (!Get("then").UnwrapTo(&thenMethod)) {
2823
+ return Nothing<Promise>();
2824
+ }
2825
+ MaybeOrValue<Value> result =
2826
+ thenMethod.As<Function>().Call(*this, {onFulfilled});
2827
+ if (result.IsJust()) {
2828
+ return Just(scope.Escape(result.Unwrap()).As<Promise>());
2829
+ }
2830
+ return Nothing<Promise>();
2831
+ #else
2832
+ Function thenMethod = Get("then").As<Function>();
2833
+ MaybeOrValue<Value> result = thenMethod.Call(*this, {onFulfilled});
2834
+ if (scope.Env().IsExceptionPending()) {
2835
+ return Promise();
2836
+ }
2837
+ return scope.Escape(result).As<Promise>();
2838
+ #endif
2839
+ }
2840
+
2841
+ inline MaybeOrValue<Promise> Promise::Then(napi_value onFulfilled,
2842
+ napi_value onRejected) const {
2843
+ EscapableHandleScope scope(_env);
2844
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
2845
+ Value thenMethod;
2846
+ if (!Get("then").UnwrapTo(&thenMethod)) {
2847
+ return Nothing<Promise>();
2848
+ }
2849
+ MaybeOrValue<Value> result =
2850
+ thenMethod.As<Function>().Call(*this, {onFulfilled, onRejected});
2851
+ if (result.IsJust()) {
2852
+ return Just(scope.Escape(result.Unwrap()).As<Promise>());
2853
+ }
2854
+ return Nothing<Promise>();
2855
+ #else
2856
+ Function thenMethod = Get("then").As<Function>();
2857
+ MaybeOrValue<Value> result =
2858
+ thenMethod.Call(*this, {onFulfilled, onRejected});
2859
+ if (scope.Env().IsExceptionPending()) {
2860
+ return Promise();
2861
+ }
2862
+ return scope.Escape(result).As<Promise>();
2863
+ #endif
2864
+ }
2865
+
2866
+ inline MaybeOrValue<Promise> Promise::Catch(napi_value onRejected) const {
2867
+ EscapableHandleScope scope(_env);
2868
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
2869
+ Value catchMethod;
2870
+ if (!Get("catch").UnwrapTo(&catchMethod)) {
2871
+ return Nothing<Promise>();
2872
+ }
2873
+ MaybeOrValue<Value> result =
2874
+ catchMethod.As<Function>().Call(*this, {onRejected});
2875
+ if (result.IsJust()) {
2876
+ return Just(scope.Escape(result.Unwrap()).As<Promise>());
2877
+ }
2878
+ return Nothing<Promise>();
2879
+ #else
2880
+ Function catchMethod = Get("catch").As<Function>();
2881
+ MaybeOrValue<Value> result = catchMethod.Call(*this, {onRejected});
2882
+ if (scope.Env().IsExceptionPending()) {
2883
+ return Promise();
2884
+ }
2885
+ return scope.Escape(result).As<Promise>();
2886
+ #endif
2887
+ }
2888
+
2889
+ inline MaybeOrValue<Promise> Promise::Then(const Function& onFulfilled) const {
2890
+ return Then(static_cast<napi_value>(onFulfilled));
2891
+ }
2892
+
2893
+ inline MaybeOrValue<Promise> Promise::Then(const Function& onFulfilled,
2894
+ const Function& onRejected) const {
2895
+ return Then(static_cast<napi_value>(onFulfilled),
2896
+ static_cast<napi_value>(onRejected));
2897
+ }
2898
+
2899
+ inline MaybeOrValue<Promise> Promise::Catch(const Function& onRejected) const {
2900
+ return Catch(static_cast<napi_value>(onRejected));
2901
+ }
2902
+
2816
2903
  ////////////////////////////////////////////////////////////////////////////////
2817
2904
  // Buffer<T> class
2818
2905
  ////////////////////////////////////////////////////////////////////////////////
package/napi.h CHANGED
@@ -1574,7 +1574,18 @@ class Promise : public Object {
1574
1574
 
1575
1575
  static void CheckCast(napi_env env, napi_value value);
1576
1576
 
1577
+ Promise();
1577
1578
  Promise(napi_env env, napi_value value);
1579
+
1580
+ MaybeOrValue<Promise> Then(napi_value onFulfilled) const;
1581
+ MaybeOrValue<Promise> Then(napi_value onFulfilled,
1582
+ napi_value onRejected) const;
1583
+ MaybeOrValue<Promise> Catch(napi_value onRejected) const;
1584
+
1585
+ MaybeOrValue<Promise> Then(const Function& onFulfilled) const;
1586
+ MaybeOrValue<Promise> Then(const Function& onFulfilled,
1587
+ const Function& onRejected) const;
1588
+ MaybeOrValue<Promise> Catch(const Function& onRejected) const;
1578
1589
  };
1579
1590
 
1580
1591
  template <typename T>
package/package.json CHANGED
@@ -472,7 +472,7 @@
472
472
  "lint:fix": "eslint --fix && node tools/clang-format --fix"
473
473
  },
474
474
  "pre-commit": "lint",
475
- "version": "8.4.0",
475
+ "version": "8.5.0",
476
476
  "support": true,
477
477
  "engines": {
478
478
  "node": "^18 || ^20 || >= 21"