@@ -73,18 +73,10 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_async(uv_as
7373 Nan::TryCatch tryCatch;
7474 Local<v8::Value> result = callback->Call ({{ cbFunction.args |jsArgsCount }}, argv);
7575
76- if (result->IsObject () && Nan::Has (result->ToObject (), Nan::New (" then" ).ToLocalChecked ()).FromJust ()) {
77- Local<v8::Value> thenProp = Nan::Get (result->ToObject (), Nan::New (" then" ).ToLocalChecked ()).ToLocalChecked ();
78-
79- if (thenProp->IsFunction ()) {
80- // we can be reasonbly certain that the result is a promise
81- Local<Object> promise = result->ToObject ();
82-
83- baton->promise .Reset (promise);
76+ uv_close ((uv_handle_t *) &baton->req , NULL );
8477
85- uv_close ((uv_handle_t *) &baton->req , (uv_close_cb) {{ cppFunctionName}}_{{ cbFunction.name }}_setupAsyncPromisePolling);
86- return ;
87- }
78+ if (PromiseCompletion::ForwardIfPromise (result, baton, {{ cppFunctionName }}_{{ cbFunction.name }}_promiseCompleted)) {
79+ return ;
8880 }
8981
9082 {% each cbFunction|returnsInfo false true as _return %}
@@ -113,36 +105,14 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_async(uv_as
113105 {% endeach %}
114106
115107 baton->done = true ;
116- uv_close ((uv_handle_t *) &baton->req , NULL );
117- }
118-
119- void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_setupAsyncPromisePolling(uv_async_t * req) {
120- {{ cppFunctionName }}_{{ cbFunction.name |titleCase }}Baton* baton = static_cast <{{ cppFunctionName }}_{{ cbFunction.name |titleCase }}Baton*>(req->data );
121- uv_async_init (uv_default_loop (), &baton->req , (uv_async_cb) {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling);
122- uv_async_send (&baton->req );
123108}
124109
125- void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling( uv_async_t * req, int status ) {
110+ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_promiseCompleted( bool isFulfilled, AsyncBaton *_baton, v8::Local<v8::Value> result ) {
126111 Nan::HandleScope scope;
127112
128- {{ cppFunctionName }}_{{ cbFunction.name |titleCase }}Baton* baton = static_cast <{{ cppFunctionName }}_{{ cbFunction.name |titleCase }}Baton*>(req->data );
129- Local<Object> promise = Nan::New<Object>(baton->promise );
130- Nan::Callback* isPendingFn = new Nan::Callback (Nan::Get (promise, Nan::New (" isPending" ).ToLocalChecked ()).ToLocalChecked ().As <Function>());
131- Local<Value> argv[1 ]; // MSBUILD won't assign an array of length 0
132- Local<Boolean> isPending = isPendingFn->Call (promise, 0 , argv)->ToBoolean ();
133-
134- if (isPending->Value ()) {
135- uv_async_send (&baton->req );
136- return ;
137- }
138-
139- Nan::Callback* isFulfilledFn = new Nan::Callback (Nan::Get (promise, Nan::New (" isFulfilled" ).ToLocalChecked ()).ToLocalChecked ().As <Function>());
140- Local<Boolean> isFulfilled = isFulfilledFn->Call (promise, 0 , argv)->ToBoolean ();
141-
142- if (isFulfilled->Value ()) {
143- Nan::Callback* resultFn = new Nan::Callback (Nan::Get (promise, Nan::New (" value" ).ToLocalChecked ()).ToLocalChecked ().As <Function>());
144- Local<v8::Value> result = resultFn->Call (promise, 0 , argv);
113+ {{ cppFunctionName }}_{{ cbFunction.name |titleCase }}Baton* baton = static_cast <{{ cppFunctionName }}_{{ cbFunction.name |titleCase }}Baton*>(_baton);
145114
115+ if (isFulfilled) {
146116 {% each cbFunction|returnsInfo false true as _return %}
147117 if (result.IsEmpty () || result->IsNativeError ()) {
148118 baton->result = {{ cbFunction.return .error }};
@@ -167,23 +137,18 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromis
167137 baton->result = {{ cbFunction.return .noResults }};
168138 }
169139 {% endeach %}
170- baton->done = true ;
171140 }
172141 else {
173142 // promise was rejected
174143 {{ cppClassName }}* instance = static_cast <{{ cppClassName }}*>(baton->{% each cbFunction.args |argsInfo as arg %}
175144 {% if arg.payload == true %}{{arg.name }}{% elsif arg.lastArg %}{{arg.name }}{% endif %}
176145 {% endeach %});
177146 Local<v8::Object> parent = instance->handle ();
178- Nan::Callback* reasonFn = new Nan::Callback (Nan::Get (promise, Nan::New (" reason" ).ToLocalChecked ()).ToLocalChecked ().As <Function>());
179- Local<v8::Value> reason = reasonFn->Call (promise, 0 , argv);
180- parent->SetHiddenValue (Nan::New (" NodeGitPromiseError" ).ToLocalChecked (), reason);
147+ parent->SetHiddenValue (Nan::New (" NodeGitPromiseError" ).ToLocalChecked (), result);
181148
182149 baton->result = {{ cbFunction.return .error }};
183- baton->done = true ;
184150 }
185-
186- uv_close ((uv_handle_t *) &baton->req , NULL );
151+ baton->done = true ;
187152}
188153 {%endif%}
189154{%endeach%}
0 commit comments