diff --git a/index.html b/index.html
index 5cea830..11f771f 100644
--- a/index.html
+++ b/index.html
@@ -727,8 +727,9 @@
Description:
- Measures the time that takes for the decorated method to response.
- By default will log the result using console.info() but this can be changed by providing your own
+ Measures the time that takes for the decorated method to respond.
+ Also returns the original response.
+ By default, will log the result using console.info() but this can be changed by providing your own
reporter
function.
diff --git a/src/common/utils/utils.ts b/src/common/utils/utils.ts
index 082b3ad..5042ae8 100644
--- a/src/common/utils/utils.ts
+++ b/src/common/utils/utils.ts
@@ -1,5 +1,5 @@
export function isPromise(obj: any): obj is Promise
{
- return !!(obj && obj.then !== undefined);
+ return !!(obj && typeof obj.then === 'function');
}
export function sleep(n: number): Promise {
diff --git a/src/exec-time/exec-time.spec.ts b/src/exec-time/exec-time.spec.ts
index ecd2c2f..209d211 100644
--- a/src/exec-time/exec-time.spec.ts
+++ b/src/exec-time/exec-time.spec.ts
@@ -31,11 +31,12 @@ describe('exec-time', () => {
}
const t = new T();
- t.foo('a');
+ const returned = t.foo('a');
expect(reporter).toHaveBeenCalledTimes(1);
const args: ExactTimeReportData = reporter.mock.calls[0][0];
expect(args.args).toEqual(['a']);
+ expect(returned).toEqual('ab');
expect(args.result).toEqual('ab');
expect(args.execTime).toBeGreaterThanOrEqual(0);
expect(args.execTime).toBeLessThan(10);
@@ -54,11 +55,12 @@ describe('exec-time', () => {
}
const t = new T();
- await t.foo('a');
+ const returned = await t.foo('a');
expect(reporter).toHaveBeenCalledTimes(1);
const args: ExactTimeReportData = reporter.mock.calls[0][0];
expect(args.args).toEqual(['a']);
+ expect(returned).toEqual('ab');
expect(args.result).toEqual('ab');
expect(args.execTime).toBeGreaterThanOrEqual(8);
expect(args.execTime).toBeLessThan(20);
@@ -75,14 +77,15 @@ describe('exec-time', () => {
}
const t = new T();
- await t.foo('a');
+ const returned = await t.foo('a');
expect(logSpy).toHaveBeenCalledTimes(1);
const clogSpyArgs = logSpy.mock.calls[0][0];
expect(clogSpyArgs).toBeGreaterThanOrEqual(0);
+ expect(returned).toEqual('ab');
logSpy.mockRestore();
});
- it('should make sure that the reporter is called when provided as sting', async () => {
+ it('should make sure that the reporter is called when provided as string', async () => {
class T {
goo = jest.fn();
@@ -95,11 +98,12 @@ describe('exec-time', () => {
}
const t = new T();
- await t.foo('a');
+ const returned = await t.foo('a');
expect(t.goo).toHaveBeenCalledTimes(1);
const args: ExactTimeReportData = t.goo.mock.calls[0][0];
expect(args.args).toEqual(['a']);
+ expect(returned).toEqual('ab');
expect(args.result).toEqual('ab');
expect(args.execTime).toBeGreaterThanOrEqual(8);
expect(args.execTime).toBeLessThan(20);
diff --git a/src/exec-time/exec-timify.ts b/src/exec-time/exec-timify.ts
index 4b23a3d..920036c 100644
--- a/src/exec-time/exec-timify.ts
+++ b/src/exec-time/exec-timify.ts
@@ -10,16 +10,24 @@ const reporter: ReportFunction = function (data: ExactTimeReportData): void {
export function execTimify(
originalMethod: Method | AsyncMethod,
arg?: ReportFunction | string,
-): AsyncMethod {
+): typeof originalMethod {
const input: ReportFunction | string = arg ?? reporter;
- return async function (...args: A): Promise {
+ return function (...args: A) {
const repFunc: ReportFunction = typeof input === 'string' ? this[input].bind(this) : input;
const start = Date.now();
- let result = originalMethod.apply(this, args);
+ const result = originalMethod.apply(this, args);
if (isPromise(result)) {
- result = await result;
+ return result.then(resolved => {
+ repFunc({
+ args,
+ result: resolved,
+ execTime: Date.now() - start,
+ });
+
+ return resolved;
+ });
}
repFunc({
@@ -27,5 +35,7 @@ export function execTimify(
result,
execTime: Date.now() - start,
});
+
+ return result;
};
}