diff --git a/src/benchmark.js b/src/benchmark.js index 933fc12..a71fc40 100644 --- a/src/benchmark.js +++ b/src/benchmark.js @@ -29,7 +29,12 @@ import { } from './reporter/terminal/index.js' import { runtime } from './runtime.js' import { now } from './time.js' -import { isAsyncFnResource, isFunction, isObject } from './utils.js' +import { + isAsyncFnResource, + isAsyncFunction, + isFunction, + isObject, +} from './utils.js' let groupName = null const groups = new Map() @@ -75,6 +80,10 @@ export function group(name, cb = undefined) { } if (!isFunction(cb)) throw new TypeError(`expected function, got ${cb.constructor.name}`) + if (isAsyncFunction(cb)) + throw new TypeError( + `expected synchronous function, got asynchronous function ${cb.constructor.name}` + ) if (isObject(name)) { if (name.name != null && 'string' !== typeof name.name) throw new TypeError( @@ -127,14 +136,8 @@ export function group(name, cb = undefined) { before: name.before ?? emptyFunction, after: name.after ?? emptyFunction, }) - if (isAsyncFnResource(cb)) { - cb().then(() => { - groupName = null - }) - } else { - cb() - groupName = null - } + cb() + groupName = null } /** @@ -362,7 +365,7 @@ export async function run(opts = {}) { if (once) log('') log(groupHeader(groupName, opts)) } - isAsyncFnResource(groupOpts.before) + isAsyncFunction(groupOpts.before) ? await groupOpts.before() : groupOpts.before() @@ -372,7 +375,7 @@ export async function run(opts = {}) { opts, groupOpts ) - isAsyncFnResource(groupOpts.after) + isAsyncFunction(groupOpts.after) ? await groupOpts.after() : groupOpts.after() } diff --git a/src/index.d.ts b/src/index.d.ts index f23dc7d..13f6710 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -9,12 +9,9 @@ export type GroupOptions = { after?: () => void | Promise } -export function group(cb: () => void | Promise): void -export function group(name: string, cb: () => void | Promise): void -export function group( - options: GroupOptions, - cb: () => void | Promise -): void +export function group(cb: () => void): void +export function group(name: string, cb: () => void): void +export function group(options: GroupOptions, cb: () => void): void export type BenchmarkOptions = { samples?: number // minimum number of samples diff --git a/src/lib.js b/src/lib.js index 74d701f..085f198 100644 --- a/src/lib.js +++ b/src/lib.js @@ -22,6 +22,7 @@ import { AsyncFunction, checkDividend, isAsyncFnResource, + isAsyncFunction, isFunction, isObject, } from './utils.js' @@ -216,8 +217,8 @@ export async function measure(fn, opts = {}) { opts.before = opts.before ?? emptyFunction opts.after = opts.after ?? emptyFunction - const asyncBefore = isAsyncFnResource(opts.before) - const asyncAfter = isAsyncFnResource(opts.after) + const asyncBefore = isAsyncFunction(opts.before) + const asyncAfter = isAsyncFunction(opts.after) const asyncFunction = opts.async || asyncBefore || asyncAfter diff --git a/src/utils.js b/src/utils.js index 39682aa..41f0aad 100644 --- a/src/utils.js +++ b/src/utils.js @@ -27,7 +27,7 @@ export const AsyncFunction = (async () => {}).constructor * @param {Function} fn the function to check * @returns {Boolean} true if the function is an async function */ -const isAsyncFunction = fn => { +export const isAsyncFunction = fn => { return AsyncFunction === fn?.constructor } @@ -49,7 +49,7 @@ export const isAsyncFnResource = fn => { const promiseLike = isPromiseLike(fnCall) if (promiseLike) { // silence promise rejection - fnCall.catch(() => {}) + fnCall.then(() => {}).catch(() => {}) } return promiseLike } catch {