Skip to content

Commit 88ad5b9

Browse files
feat: add Then and Catch methods with Function overloads
1 parent 6b1ea6f commit 88ad5b9

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

napi-inl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,6 +2879,18 @@ inline MaybeOrValue<Promise> Promise::Catch(napi_value onRejected) const {
28792879
#endif
28802880
}
28812881

2882+
inline MaybeOrValue<Promise> Promise::Then(const Function& onFulfilled) const {
2883+
return Then(static_cast<napi_value>(onFulfilled));
2884+
}
2885+
2886+
inline MaybeOrValue<Promise> Promise::Then(const Function& onFulfilled, const Function& onRejected) const {
2887+
return Then(static_cast<napi_value>(onFulfilled), static_cast<napi_value>(onRejected));
2888+
}
2889+
2890+
inline MaybeOrValue<Promise> Promise::Catch(const Function& onRejected) const {
2891+
return Catch(static_cast<napi_value>(onRejected));
2892+
}
2893+
28822894
////////////////////////////////////////////////////////////////////////////////
28832895
// Buffer<T> class
28842896
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,10 @@ class Promise : public Object {
15801580
MaybeOrValue<Promise> Then(napi_value onFulfilled) const;
15811581
MaybeOrValue<Promise> Then(napi_value onFulfilled, napi_value onRejected) const;
15821582
MaybeOrValue<Promise> Catch(napi_value onRejected) const;
1583+
1584+
MaybeOrValue<Promise> Then(const Function& onFulfilled) const;
1585+
MaybeOrValue<Promise> Then(const Function& onFulfilled, const Function& onRejected) const;
1586+
MaybeOrValue<Promise> Catch(const Function& onRejected) const;
15831587
};
15841588

15851589
template <typename T>

test/promise.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ async function test (binding) {
3535
assert.strictEqual(thenRejected.isPromise, true);
3636
const rejectedValue = await thenRejected.promise;
3737
assert.strictEqual(rejectedValue, 'Rejected!');
38+
39+
const catchMethod = binding.promise.catchMethod(onRejected);
40+
assert.strictEqual(catchMethod.isPromise, true);
41+
const catchValue = await catchMethod.promise;
42+
assert.strictEqual(catchValue, 'Rejected!');
3843
}

0 commit comments

Comments
 (0)