1 # onetime [![Build Status](https://travis-ci.org/sindresorhus/onetime.svg?branch=master)](https://travis-ci.org/sindresorhus/onetime)
3 > Ensure a function is only called once
5 When called multiple times it will return the return value from the first call.
7 *Unlike the module [once](https://github.com/isaacs/once), this one isn't naughty and extending `Function.prototype`.*
20 const onetime = require('onetime');
24 const foo = onetime(() => ++i);
30 onetime.callCount(foo); //=> 3
34 const onetime = require('onetime');
36 const foo = onetime(() => {}, {throw: true});
41 //=> Error: Function `foo` can only be called once
47 ### onetime(fn, [options])
49 Returns a function that only calls `fn` once.
55 Function that should only be called once.
66 Throw an error when called more than once.
68 ### onetime.callCount(fn)
70 Returns a number representing how many times `fn` has been called.
72 Note: It throws an error if you pass in a function that is not wrapped by `onetime`.
75 const foo = onetime(() => {});
81 console.log(onetime.callCount(foo));
89 Function to get call count from.
94 MIT © [Sindre Sorhus](https://sindresorhus.com)