Skip to content

Commit

Permalink
Improve error message when not passing in a function for the first pa…
Browse files Browse the repository at this point in the history
…rameter

Closes #8
  • Loading branch information
sindresorhus committed Jun 21, 2024
1 parent cd2c05c commit 69d2486
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function throttle(function_, wait) {
if (typeof function_ !== 'function') {
throw new TypeError(`Expected the first argument to be a \`function\`, got \`${typeof function_}\`.`);
}

// TODO: Add `wait` validation too in the next major version.

let timeoutId;
let lastCallTime = 0;

Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,13 @@ test('throttled function handles system time changes', async t => {
await delay(wait);
t.is(count.callCount, 2, 'Should allow a call after wait time');
});

test('parameter validation', t => {
t.throws(() => {
throttle(undefined, 0);
}, {instanceOf: TypeError});

/// t.throws(() => {
// throttle(() => {});
// }, {instanceOf: TypeError});
});

0 comments on commit 69d2486

Please sign in to comment.