Implement once
Write once(fn): call fn on the first call only, with that call's arguments and this. Every later call returns the first result without calling fn again, even when that result was undefined.
const init = once(() => setup()); init(); init(); init(); // setup runs once; every call returns its result
Hints
Hint 1
once needs a flag, not a check on the result: if (!called), never if (result === undefined).
Hint 2
Return a function, not an arrow, and call fn.apply(this, args) so the first call's this reaches fn.
Asked at
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.