ts-prime
Version:
A utility library for JavaScript and Typescript.
26 lines (25 loc) • 659 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Creates a function that is restricted to invoking `func` once. Repeat calls to the function return the value of the first invocation.
* @param fn the function to wrap
* @signature P.once(fn)
* @example
* const initialize = P.once(createApplication);
* initialize();
* initialize();
* // => `createApplication` is invoked once
* @category Function
*/
function once(fn) {
var called = false;
var ret;
return function () {
if (!called) {
ret = fn();
called = true;
}
return ret;
};
}
exports.once = once;