Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support pending(fn: Callback, n: number) #7

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading