Skip to content

Commit 6b1ea6f

Browse files
feat: add Catch method to Promise class
1 parent a8ae1fa commit 6b1ea6f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

napi-inl.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,28 @@ inline MaybeOrValue<Promise> Promise::Then(napi_value onFulfilled, napi_value on
28572857
#endif
28582858
}
28592859

2860+
inline MaybeOrValue<Promise> Promise::Catch(napi_value onRejected) const {
2861+
EscapableHandleScope scope(_env);
2862+
#ifdef NODE_ADDON_API_ENABLE_MAYBE
2863+
MaybeOrValue<Value> catchMethodMaybe = Get("catch");
2864+
Function catchMethod = catchMethodMaybe.Unwrap().As<Function>();
2865+
#else
2866+
Function catchMethod = Get("catch").As<Function>();
2867+
#endif
2868+
MaybeOrValue<Value> result = catchMethod.Call(*this, {onRejected});
2869+
#ifdef NODE_ADDON_API_ENABLE_MAYBE
2870+
if (result.IsJust()) {
2871+
return Just(scope.Escape(result.Unwrap()).As<Promise>());
2872+
}
2873+
return Nothing<Promise>();
2874+
#else
2875+
if (scope.Env().IsExceptionPending()) {
2876+
return Promise();
2877+
}
2878+
return scope.Escape(result).As<Promise>();
2879+
#endif
2880+
}
2881+
28602882
////////////////////////////////////////////////////////////////////////////////
28612883
// Buffer<T> class
28622884
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@ class Promise : public Object {
15791579

15801580
MaybeOrValue<Promise> Then(napi_value onFulfilled) const;
15811581
MaybeOrValue<Promise> Then(napi_value onFulfilled, napi_value onRejected) const;
1582+
MaybeOrValue<Promise> Catch(napi_value onRejected) const;
15821583
};
15831584

15841585
template <typename T>

test/promise.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ Value ThenMethodOnFulfilledOnRejectedReject(const CallbackInfo& info) {
7474
return result;
7575
}
7676

77+
Value CatchMethod(const CallbackInfo& info) {
78+
auto deferred = Promise::Deferred::New(info.Env());
79+
Function onRejected = info[0].As<Function>();
80+
81+
Promise resultPromise = MaybeUnwrap(deferred.Promise().Catch(onRejected));
82+
83+
bool isPromise = resultPromise.IsPromise();
84+
deferred.Reject(String::New(info.Env(), "Rejected"));
85+
86+
Object result = Object::New(info.Env());
87+
result["isPromise"] = Boolean::New(info.Env(), isPromise);
88+
result["promise"] = resultPromise;
89+
90+
return result;
91+
}
92+
7793
Object InitPromise(Env env) {
7894
Object exports = Object::New(env);
7995

@@ -87,6 +103,7 @@ Object InitPromise(Env env) {
87103
Function::New(env, ThenMethodOnFulfilledOnRejectedResolve);
88104
exports["thenMethodOnFulfilledOnRejectedReject"] =
89105
Function::New(env, ThenMethodOnFulfilledOnRejectedReject);
106+
exports["catchMethod"] = Function::New(env, CatchMethod);
90107

91108
return exports;
92109
}

0 commit comments

Comments
 (0)