7.0.0 • Published 1 year ago

onetime v7.0.0

Weekly downloads
29,506,755
License
MIT
Repository
github
Last release
1 year ago

onetime

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

import onetime from 'onetime';

let index = 0;

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

foo(); //=> 1
foo(); //=> 1
foo(); //=> 1

onetime.callCount(foo); //=> 3
import onetime from 'onetime';

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

The 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.

import onetime from 'onetime';

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

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

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

fn

Type: Function

The function to get call count from.

restore-cursorfree-min@huyhpham/rn-linearchetype-librarycomponennentteasy-select-rnvuedragdropuploadimagesreact-native-bluetooth2killi8n-react-native-fast-imagepipihomern-send-smsspecify-importsbabel-specify-imports@icanpm/api-master@arisageha/react-lazyload@arisageha/react-lazyload-fix@oneplanetcrowd/developersrdclr-boilerplate@almeidaa/msreact-native-template-rfbaseairscanairscan-examplebb-chatreact-native-esc-pos-sahaab@borisovart/atol-kkt-module@frxf/frxfdeneme323112@texttree/demo-bsa-reference-rcl@fundefund/funde_ck@ntt_app/react-native-custom-notificationreact-native-custom-text-hwjamesreact-native-covid-sdkgql_din_modbitgetreact-native-thanh-toast-librarymutasi-bca@jttechnic/interpreter@thanhnguyen14797/react-native-thanh-toast-libraryauto-extractcthpb-plugin-social@iobroker-community-adapters/iobroker.device-watcher@olivervorasai/slidermysql-formatreact-native-printer-brothersrn-pdf-reader-offlinecbmis-ai-toolbox@newhorizon-tech/dd-npm-package-templatereact-native-shekhar-bridge-testcogoportutilsukor-remasterdyx-reactlevibestliblevibestlib2levilibtest19levilibtest24levilibtest25levilibtest26levilibtest27levilibtest28levilibtest29talent-to-vite-cliwilscanner@oiti/documentoscopy-react-nativejs4cytoscape@respondea/cordova-plugin-v-inappbrowser@mink-opn/build-tokensquoc-testreact-native-slider-kfunblock-block-save-variables@prodam/prodam-typescclibyarntest@saaspe/componentshyperpass-sdkplginshamsi-date-nowexpand-react-bridgeopea-bootstraapluminos-ui-coresklif-ui-kitsklif-apiiqra-calculatorjawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgp149-table@rabailriaz/hisaab-web-portalsklif-uircf-tea-testnetreact-native-jawwy_samplecode-msi-simple_calculator123zs89cli-number-guessingkny63-atm-machinekshahmeer10_atmatm_process@uf3sindh/simple_cli_calculatorhmmmmd12-cli-number-guessinghmd12-cli-number-guessing
7.0.0

1 year ago

6.0.0

4 years ago

5.1.2

4 years ago

5.1.1

4 years ago

5.1.0

6 years ago

5.0.0

6 years ago

4.0.0

6 years ago

3.0.0

6 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.0

9 years ago

1.0.0

10 years ago

0.1.1

11 years ago

0.1.0

11 years ago