• Stars
    star
    216
  • Rank 175,360 (Top 4 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created over 11 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run a function exactly one time

once

Only call a function once.

usage

var once = require('once')

function load (file, cb) {
  cb = once(cb)
  loader.load('file')
  loader.once('load', cb)
  loader.once('error', cb)
}

Or add to the Function.prototype in a responsible way:

// only has to be done once
require('once').proto()

function load (file, cb) {
  cb = cb.once()
  loader.load('file')
  loader.once('load', cb)
  loader.once('error', cb)
}

Ironically, the prototype feature makes this module twice as complicated as necessary.

To check whether your function has been called, use fn.called. Once the function is called for the first time the return value of the original function is saved in fn.value and subsequent calls will continue to return this value.

var once = require('once')

function load (cb) {
  cb = once(cb)
  var stream = createStream()
  stream.once('data', cb)
  stream.once('end', function () {
    if (!cb.called) cb(new Error('not found'))
  })
}

once.strict(func)

Throw an error if the function is called twice.

Some functions are expected to be called only once. Using once for them would potentially hide logical errors.

In the example below, the greet function has to call the callback only once:

function greet (name, cb) {
  // return is missing from the if statement
  // when no name is passed, the callback is called twice
  if (!name) cb('Hello anonymous')
  cb('Hello ' + name)
}

function log (msg) {
  console.log(msg)
}

// this will print 'Hello anonymous' but the logical error will be missed
greet(null, once(msg))

// once.strict will print 'Hello anonymous' and throw an error when the callback will be called the second time
greet(null, once.strict(msg))

More Repositories

1

node-glob

glob functionality for node.js
TypeScript
8,123
star
2

rimraf

A `rm -rf` util for nodejs
JavaScript
5,309
star
3

node-lru-cache

A fast cache that automatically deletes the least recently used items
TypeScript
4,844
star
4

minimatch

a glob matcher in javascript
JavaScript
3,074
star
5

github

Just a place to track issues and feature requests that I have for github
2,196
star
6

nave

Virtual Environments for Node
Shell
1,580
star
7

node-graceful-fs

fs with incremental backoff on EMFILE
JavaScript
1,254
star
8

sax-js

A sax style parser for JS
JavaScript
1,046
star
9

node-tar

tar for node
JavaScript
755
star
10

tshy

JavaScript
653
star
11

st

A node module for serving static files. Does etags, caching, etc.
JavaScript
376
star
12

inherits

Easy simple tiny inheritance in JavaScript
JavaScript
352
star
13

cluster-master

Take advantage of node built-in cluster module behavior
JavaScript
276
star
14

minipass

A stream implementation that does more by doing less
TypeScript
237
star
15

yallist

Yet Another Linked List
JavaScript
198
star
16

server-destroy

When close() is just not enough
JavaScript
184
star
17

semicolons

When you require("semicolons"), THEY ARE REQUIRED.
JavaScript
145
star
18

slide-flow-control

A flow control library that fits in a slideshow
JavaScript
134
star
19

treeverse

Walk any kind of tree structure depth- or breadth-first. Supports promises and advanced map-reduce operations with a very small API.
JavaScript
126
star
20

multipart-js

JavaScript
123
star
21

reading-list

a list of books I recommend
121
star
22

node-touch

touch(1) for node
JavaScript
121
star
23

async-cache

Cache your async lookups and don't fetch the same thing more than necessary.
JavaScript
119
star
24

catcher

TypeScript
116
star
25

ttlcache

TypeScript
116
star
26

core-util-is

The util.is* functions from Node core
JavaScript
98
star
27

dezalgo

Contain async insanity so that the dark pony lord doesn't eat souls
JavaScript
89
star
28

github-flavored-markdown

Deprecated. Use marked instead.
JavaScript
79
star
29

node-bench

JavaScript
71
star
30

free-as-in-hugs-license

A (Not OSI-Approved) software license you may use if you wish
70
star
31

sigmund

Quick and dirty psychoanalysis for objects
JavaScript
67
star
32

minizlib

A smaller, faster, zlib stream built on http://npm.im/minipass and Node.js's zlib binding.
JavaScript
66
star
33

inflight

Add callbacks to requests in flight to avoid async duplication
JavaScript
66
star
34

fast-list

A fast O(1) push/pop/shift/unshift thing
JavaScript
66
star
35

gist-cli

A gist cli client written in Node
JavaScript
64
star
36

dotfiles

My Dot Files
Shell
63
star
37

wrappy

Callback wrapping utility
JavaScript
56
star
38

block-stream

A stream of fixed-size blocks
JavaScript
52
star
39

isexe

Minimal module to check if a file is executable.
TypeScript
48
star
40

.vim

My vim settings
Vim Script
47
star
41

char-spinner

Put a little spinner on process.stderr, as unobtrusively as possible.
JavaScript
43
star
42

st-example

an example of serving static files easily in node using the st module
JavaScript
40
star
43

jackspeak

A very strict and proper argument parser.
TypeScript
38
star
44

templar

A lightweight template thing for node http servers
JavaScript
37
star
45

nosync

Prevent sync functions in your node programs after first tick
JavaScript
37
star
46

use-strict

Makes all subsequent modules in Node get loaded in strict mode.
JavaScript
37
star
47

ssh-key-decrypt

Decrypt and encrypted ssh private keys
JavaScript
35
star
48

ejsgi

Like JSGI, but using streams.
JavaScript
35
star
49

node-eliza

A Robotic Rogerian Therapist, on IRC
JavaScript
34
star
50

natives

Do stuff with Node.js's native JavaScript modules
JavaScript
31
star
51

goosh

Front-end old-style terminal interface, for web services like those provided by Google and Yahoo.
JavaScript
31
star
52

simple-node-server

A simple fast node http server toolkit.
JavaScript
30
star
53

util-extend

Node's internal object extension function, for you!
JavaScript
30
star
54

chownr

Like `chown -R`
JavaScript
28
star
55

csrf-lite

CSRF protection utility for framework-free node sites.
JavaScript
28
star
56

chmodr

Like `chmod -R` in node
JavaScript
28
star
57

path-scurry

TypeScript
27
star
58

node-hexedit

hexadecimal editor in node
JavaScript
27
star
59

back-to-markdown.css

Turns any markdown editor into a WYSIWYG editor
CSS
26
star
60

node-async-simple

Multiply two numbers, slowly, on the thread pool.
C++
26
star
61

node-strict

Makes your Node programs strict about stuff when loaded
JavaScript
25
star
62

json-stringify-nice

Stringify an object sorting scalars before objects, and defaulting to 2-space indent
JavaScript
25
star
63

promise-all-reject-late

Like Promise.all, but save rejections until all promises are resolved
JavaScript
24
star
64

promise-call-limit

Call an array of promise-returning functions, restricting concurrency to a specified limit.
TypeScript
24
star
65

fs.realpath

Use node's fs.realpath, but fall back to the JS implementation if the native one fails
JavaScript
24
star
66

node6-module-system-change

A demonstration of what changed in node 6's module loading logic
JavaScript
24
star
67

color-support

A module which will endeavor to guess your terminal's level of color support.
JavaScript
24
star
68

polite-json

TypeScript
23
star
69

ircretary

A note-taking IRC bot
JavaScript
23
star
70

yamlish

A parser for the yamlish format
JavaScript
22
star
71

sock-daemon

TypeScript
21
star
72

pseudomap

Like `new Map` but for older JavaScripts
JavaScript
21
star
73

node-fuse

Fuse bindings for nodejs
21
star
74

slocket

A locking socket alternative to file-system mutex locks
JavaScript
21
star
75

proto-list

A list of objects bound by prototype chain
JavaScript
20
star
76

retry-until

A function that will keep running a function you give it as long as it throws for a period of time
JavaScript
20
star
77

node-srand

srand bindings for node - Seedable predictable pseudorandom number generator
C++
20
star
78

mutate-fs

Mutate the Node.js filesystem behavior for tests.
JavaScript
20
star
79

ryp

Featureless npm-package bundling.
Shell
19
star
80

filewatcherthing

a thing to watch a file and then run a command
JavaScript
19
star
81

gatsby-remark-tumble-media

A plugin for gatsby-transformer-remark to support photosets, video, and audio in markdown frontmatter.
JavaScript
19
star
82

sodn

SOcial DNodes
JavaScript
19
star
83

joyent-node-on-smart-example

A blog post.
JavaScript
18
star
84

error-page

Easily send errors in Node.js HTTP servers. Think like the `ErrorDocument` declarations in Apache config files.
JavaScript
17
star
85

_ify

an itty bitty curry utility
JavaScript
17
star
86

url-parse-as-address

Parse a URL assuming that it's http/https, even if protocol or // isn't present
JavaScript
17
star
87

http-https

A wrapper that chooses http or https for requests
JavaScript
17
star
88

perfalize

TypeScript
16
star
89

cssmin

A cross-platform regular-expression based minifier for CSS
16
star
90

duplex-passthrough

like a passthrough, but in both directions
JavaScript
16
star
91

mintee

a tiny module for piping an input to multiple output streams
JavaScript
16
star
92

tap-assert

An assert module that outputs tap result objects
JavaScript
16
star
93

create-isaacs

An npm init module to create modules like I do
JavaScript
16
star
94

domain-http-server

A module thingie to use domains in Express or Restify or just regular HTTP servers
JavaScript
15
star
95

canonical-host

Node module to redirect users to the canonical hostname for your site.
JavaScript
15
star
96

fs-readstream-seek

A fs.ReadStream that supports seeking to arbtrary locations within a file.
JavaScript
15
star
97

hardhttps

Slightly hardened https for node
JavaScript
14
star
98

exit-code

`process.exitCode` behavior back-ported from io.js and Node.js 0.12+
JavaScript
14
star
99

mcouch

Put your CouchDB in Manta, attachments and docs and all
JavaScript
14
star
100

emcee

A bridge between the M and C bits of MVC
JavaScript
13
star