MCPcopy Index your code
hub / github.com/deployd/deployd / wrapPromise

Function wrapPromise

lib/script.js:196–266  ·  view source on GitHub ↗
(promiseable, sandbox, events, done, sandboxRoot)

Source from the content-addressed store, hash-verified

194}
195
196function wrapPromise(promiseable, sandbox, events, done, sandboxRoot) {
197 var realPromise = promiseable;
198 var ret = null;
199 if(!promiseable.then && promiseable.promise && isPromise(promiseable.promise)) {
200 realPromise = bluebird.cast(promiseable.promise);
201 ret = { promise: realPromise, resolve: promiseable.resolve, reject: promiseable.reject };
202 }
203 if (!ret) {
204 ret = bluebird.cast(realPromise);
205 realPromise = ret;
206 }
207
208 var realThen = realPromise._then;
209 var addCallback = function () {
210 events.emit('addCallback');
211 };
212 var finishCallback = function() {
213 events.emit('finishCallback');
214 };
215
216 addCallback();
217 realPromise.then(finishCallback, finishCallback);
218
219 realPromise._then = function (onFulfilled, onRejected) {
220 var args = [undefined, undefined];
221
222 if (onFulfilled) {
223 // wrappedOnFulfilled
224 args[0] = function (res) {
225 addCallback();
226 try {
227 var result = onFulfilled.apply(this, arguments);
228 return isPromise(result)? wrapPromise(result, sandbox, events, done, sandboxRoot) : result;
229 } catch (err) {
230 sandboxRoot._error = err;
231 throw err;
232 } finally {
233 finishCallback();
234 }
235 };
236 }
237
238 if(onRejected) {
239 args[1] =
240 // wrappedOnRejected
241 function (error) {
242 if (error === sandboxRoot._error) {
243 // if we're handling a previously uncaught error, remove it
244 sandboxRoot._error = null;
245 }
246 addCallback();
247 try {
248 var result = onRejected.apply(this, arguments);
249 return isPromise(result)? wrapPromise(result, sandbox, events, done, sandboxRoot) : result;
250 } catch (err) {
251 sandboxRoot._error = wrapError(err);
252 throw err;
253 } finally {

Callers 1

wrapAsyncFunctionFunction · 0.85

Calls 4

isPromiseFunction · 0.85
addCallbackFunction · 0.85
finishCallbackFunction · 0.85
wrapErrorFunction · 0.85

Tested by

no test coverage detected