Skip to content

Commit

Permalink
fix(meter): return async function value
Browse files Browse the repository at this point in the history
  • Loading branch information
ysa23 committed Feb 29, 2024
1 parent 94b6780 commit 9d47203
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function Space({
return async () => {
const start = new Date();
try {
await func();
return await func();
} finally {
const finish = new Date();
report(key, start, finish);
Expand Down
16 changes: 16 additions & 0 deletions src/space.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ describe('Space', () => {
expect(result.constructor.name).toEqual('AsyncFunction');
});

it('should return the value from the async function execution', async () => {
const reports = [];
const reporter = new InMemoryReporter({ buffer: reports });
const metrics = new Metrics({ reporters: [reporter] });
const func = async () => {
await getPromise(1000);
return 42;
};

const wrapper = metrics.space('space.meter').meter(func);

const result = await wrapper();

expect(result).toEqual(42);
});

it('upon await successful execution, should create a report where the value is the execution time of the original function it receives as argument', async () => {
const reports = [];
const reporter = new InMemoryReporter({ buffer: reports });
Expand Down

0 comments on commit 9d47203

Please sign in to comment.