Updated to use @skatcat31's suggestion
This commit is contained in:
@ -6,15 +6,7 @@ Utilizing a closure, use a flag, `called`, and set it to `true` once the functio
|
|||||||
Allow the function to be supplied with an arbitrary number of arguments using the spread (`...`) operator.
|
Allow the function to be supplied with an arbitrary number of arguments using the spread (`...`) operator.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const once = fn => {
|
const once = fn => (called => (...args) => !called ? (called = true, fn(...args)) : undefined)()
|
||||||
let called = false;
|
|
||||||
return (...args) => {
|
|
||||||
if (!called) {
|
|
||||||
fn(...args);
|
|
||||||
called = true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
Reference in New Issue
Block a user