Skip to content

Commit

Permalink
fix: support pending(fn: Callback, n: number)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 1, 2025
1 parent 132f38b commit 741c591
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ export class ExecuteTooManyTimesError extends Error {
}
}

export function pending(n: number, fn: Callback) {
export function pending(fn: Callback, n: number): (err?: Error) => void;
export function pending(n: number, fn: Callback): (err?: Error) => void;
export function pending(n: number | Callback, fn: Callback | number): (err?: Error) => void {
let _fn: Callback;
if (typeof n === 'function') {
// keep compatibility for old version
// pending(fn, n)
const tmp = n;
n = fn as unknown as number;
fn = tmp as Callback;
_fn = n;
n = fn as number;
} else {
_fn = fn as Callback;
}

let called = false;
Expand All @@ -32,11 +36,11 @@ export function pending(n: number, fn: Callback) {
}
if (err) {
called = true;
return fn(err);
return _fn(err);
}
times++;
if (times === n) {
fn();
_fn();
} else if (times > n) {
const err = new ExecuteTooManyTimesError(n, times);
err.stack += '\n' + callStack.stack;
Expand Down
2 changes: 1 addition & 1 deletion test/pedding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('test/pedding.test.ts', () => {
});

it('should called pedding(done, 100) then done()', done => {
done = pedding(done as any, 100 as any);
done = pedding(done, 100);
for (let i = 0; i < 100; i++) {
done();
}
Expand Down

0 comments on commit 741c591

Please sign in to comment.