title: Decorator function returning undefined when using promises or callbacks
body: <p>I am trying to create a decorator in node js to log entry and exit from a function when it is being executed for example
  <code>Executing ServiceClass.sampleService with params 5,1</code>
  then after execution is finished <code>Execution of ServiceClass.sampleService completed with success/error</code></p>
  
  <p>This works fine for normal functions which return number or string or array or some object literals, but when I want to decorate a function which returns promise or callback it logs details but the values are not returned where the function is invoked.</p>
  
  <p>Here is my sample code : </p>
  
  <p><strong>logger.ts</strong> (contains decorator function)</p>
  
  <pre><code>export function performanceLog(target, name, descriptor) {
      const original = descriptor.value;
      if (typeof original === 'function') {
          descriptor.value = function (...args) {
              console.log(`Executing ${target.constructor.name}.${name} with parameters ${args}`);
              // Execute function like a function with promise
              let start: any = new Date();
              return original.apply(this, args).then(data =&gt; {
                  let end: any = new Date();
                  end = end-start;
                  console.log(`\nExecution of ${target.constructor.name}.${name} completed successfully in ${end / 1000} seconds`);
              }).catch(error =&gt; {
                  let end: any = new Date();
                  end = end-start;
                  console.log(`\nExecution of ${target.constructor.name}.${name} completed with error in ${end / 1000} seconds`);
              });
          }
      }
      return descriptor;
  }
  </code></pre>
  
  <p><strong>sampleService.ts</strong> (Regular Service class)</p>
  
  <pre><code>import { performanceLog } from '../utilities/logger';
  
  let err = false;
  export default class SampleService {
      @performanceLog
      public sum(a, b) {
          return new Promise((resolve, reject) =&gt; {
              if (err) {
                  reject(a - b);
              } else {
                  resolve(a + b);
              }
          })
      }
  }
  
  const e = new SampleService();
  e.sum(51, 6).then(data =&gt; console.log("## Data: ", data)).catch(err =&gt; console.log("##err: ", err));
  // Here data is undefined and it never goes into the catch part either
  </code></pre>
  
  <p>What am I missing here?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The similar problem exits when I have a function returning callback function</p>
  
  <pre><code>export default class SampleService {
      @performanceLog
      public sum(a, b, callback) {
          // return new Promise((resolve, reject) =&gt; {
          if (err) {
              return callback(a - b, null);
          } else {
              return callback(null, a+b);
          }
          // })
      }
  }
  
  const e = new SampleService();
  // e.sum(51, 6).then(data =&gt; console.log("## Data: ", data)).catch(err =&gt; console.log("##err: ", err));
  
  e.sum(51, 6, function (err, res) {
      console.log(`This is err  ${err} and res ${res}`);
  });
  </code></pre>