Created
June 10, 2014 05:01
-
-
Save maxov/d3c12e5ef62d2046404d to your computer and use it in GitHub Desktop.
function that makes a function that only calls once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// do cb only once | |
function do_once(cb) { | |
var has_called = false; | |
// the function that is returned | |
return function () { | |
if(!has_called) { | |
cb.apply(arguments); | |
has_called = true; | |
} | |
} | |
} | |
// usage example | |
process.once('SIGINT', do_once(function (conn) { conn.close(); })) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment