JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 113830266
  • Score
    100M100P100Q238025F
  • License MIT

Ensure a function is only called once

Package Exports

  • onetime

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (onetime) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

onetime Build Status

Ensure a function is only called once

When called multiple times it will return the return value from the first call.

Unlike the module once, this one isn't naughty and extending Function.prototype.

Install

$ npm install onetime

Usage

let i = 0;

const foo = onetime(() => i++);

foo(); //=> 0
foo(); //=> 0
foo(); //=> 0

onetime.callCount(foo); //=> 3
const foo = onetime(() => {}, {throw: true});

foo();

foo();
//=> Error: Function `foo` can only be called once

API

onetime(fn, [options])

Returns a function that only calls fn once.

fn

Type: Function

Function that should only be called once.

options

Type: Object

throw

Type: boolean
Default: false

Throw an error when called more than once.

onetime.callCount(fn)

Returns a number representing how many times fn has been called.

Note: It throws an error if you pass in a function that is not wrapped by onetime.

const foo = onetime(() => {});

foo();
foo();
foo();

console.log(onetime.callCount(foo));
//=> 3

fn

Type: Function

Function to get call count from.

License

MIT © Sindre Sorhus