diff --git a/context-legacy.js b/context-legacy.js index 0657aa7..3fc5c6d 100644 --- a/context-legacy.js +++ b/context-legacy.js @@ -127,22 +127,24 @@ Namespace.prototype.runPromise = function runPromise(fn) { this.enter(context); let promise = fn(context); + if (!promise || !promise.then || !promise.catch) { throw new Error('fn must return a promise.'); } if (DEBUG_CLS_HOOKED) { debug2(' BEFORE runPromise: ' + this.name + ' uid:' + currentUid + ' len:' + this._set.length + ' ' + - util.inspect(context)); + util.inspect(context)); } + this.exit(context); + return promise .then(result => { if (DEBUG_CLS_HOOKED) { debug2(' AFTER runPromise: ' + this.name + ' uid:' + currentUid + ' len:' + this._set.length + ' ' + util.inspect(context)); } - this.exit(context); return result; }) .catch(err => { @@ -151,7 +153,6 @@ Namespace.prototype.runPromise = function runPromise(fn) { debug2(' AFTER runPromise: ' + this.name + ' uid:' + currentUid + ' len:' + this._set.length + ' ' + util.inspect(context)); } - this.exit(context); throw err; }); }; diff --git a/context.js b/context.js index 1fad2cb..f71b0b0 100644 --- a/context.js +++ b/context.js @@ -138,12 +138,13 @@ Namespace.prototype.runPromise = function runPromise(fn) { debug2('CONTEXT-runPromise BEFORE: (' + this.name + ') currentUid:' + currentUid + ' len:' + this._set.length + ' ' + util.inspect(context)); } + this.exit(context); + return promise .then(result => { if (DEBUG_CLS_HOOKED) { debug2('CONTEXT-runPromise AFTER then: (' + this.name + ') currentUid:' + currentUid + ' len:' + this._set.length + ' ' + util.inspect(context)); } - this.exit(context); return result; }) .catch(err => { @@ -151,7 +152,6 @@ Namespace.prototype.runPromise = function runPromise(fn) { if (DEBUG_CLS_HOOKED) { debug2('CONTEXT-runPromise AFTER catch: (' + this.name + ') currentUid:' + currentUid + ' len:' + this._set.length + ' ' + util.inspect(context)); } - this.exit(context); throw err; }); };