File tree Expand file tree Collapse file tree 4 files changed +49
-0
lines changed Expand file tree Collapse file tree 4 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -2813,6 +2813,28 @@ inline void Promise::CheckCast(napi_env env, napi_value value) {
2813
2813
2814
2814
inline Promise::Promise (napi_env env, napi_value value) : Object (env, value) {}
2815
2815
2816
+ inline MaybeOrValue<Promise> Promise::Then (napi_value onFulfilled) const {
2817
+ EscapableHandleScope scope (_env);
2818
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
2819
+ MaybeOrValue<Value> thenMethodMaybe = Get (" then" );
2820
+ Function thenMethod = thenMethodMaybe.Unwrap ().As <Function>();
2821
+ #else
2822
+ Function thenMethod = Get (" then" ).As <Function>();
2823
+ #endif
2824
+ MaybeOrValue<Value> result = thenMethod.Call (*this , {onFulfilled});
2825
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
2826
+ if (result.IsJust ()) {
2827
+ return Just (scope.Escape (result.Unwrap ()).As <Promise>());
2828
+ }
2829
+ return Nothing<Promise>();
2830
+ #else
2831
+ if (scope.Env ().IsExceptionPending ()) {
2832
+ return Promise ();
2833
+ }
2834
+ return scope.Escape (result).As <Promise>();
2835
+ #endif
2836
+ }
2837
+
2816
2838
// //////////////////////////////////////////////////////////////////////////////
2817
2839
// Buffer<T> class
2818
2840
// //////////////////////////////////////////////////////////////////////////////
Original file line number Diff line number Diff line change @@ -1574,7 +1574,10 @@ 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 ;
1578
1581
};
1579
1582
1580
1583
template <typename T>
Original file line number Diff line number Diff line change 1
1
#include " napi.h"
2
+ #include " test_helper.h"
2
3
3
4
using namespace Napi ;
4
5
@@ -23,6 +24,22 @@ Value PromiseReturnsCorrectEnv(const CallbackInfo& info) {
23
24
return Boolean::New (info.Env (), deferred.Env () == info.Env ());
24
25
}
25
26
27
+ Value ThenMethodOnFulfilled (const CallbackInfo& info) {
28
+ auto deferred = Promise::Deferred::New (info.Env ());
29
+ Function onFulfilled = info[0 ].As <Function>();
30
+
31
+ Promise resultPromise = MaybeUnwrap (deferred.Promise ().Then (onFulfilled));
32
+
33
+ bool isPromise = resultPromise.IsPromise ();
34
+ deferred.Resolve (Number::New (info.Env (), 42 ));
35
+
36
+ Object result = Object::New (info.Env ());
37
+ result[" isPromise" ] = Boolean::New (info.Env (), isPromise);
38
+ result[" promise" ] = resultPromise;
39
+
40
+ return result;
41
+ }
42
+
26
43
Object InitPromise (Env env) {
27
44
Object exports = Object::New (env);
28
45
@@ -31,6 +48,7 @@ Object InitPromise(Env env) {
31
48
exports[" rejectPromise" ] = Function::New (env, RejectPromise);
32
49
exports[" promiseReturnsCorrectEnv" ] =
33
50
Function::New (env, PromiseReturnsCorrectEnv);
51
+ exports[" ThenMethodOnFulfilled" ] = Function::New (env, ThenMethodOnFulfilled);
34
52
35
53
return exports;
36
54
}
Original file line number Diff line number Diff line change @@ -17,4 +17,10 @@ async function test (binding) {
17
17
rejecting . then ( common . mustNotCall ( ) ) . catch ( common . mustCall ( ) ) ;
18
18
19
19
assert ( binding . promise . promiseReturnsCorrectEnv ( ) ) ;
20
+
21
+ const onFulfilled = ( value ) => value * 2 ;
22
+ const result = binding . promise . thenMethodOnFulfilled ( onFulfilled ) ;
23
+ assert . strictEqual ( result . isPromise , true ) ;
24
+ const finalValue = await result . promise ;
25
+ assert . strictEqual ( finalValue , 84 ) ;
20
26
}
You can’t perform that action at this time.
0 commit comments