Skip to content

Instantly share code, notes, and snippets.

@taka011239
Last active October 27, 2015 23:29
Show Gist options
  • Save taka011239/ee38f58df50491da87f7 to your computer and use it in GitHub Desktop.
Save taka011239/ee38f58df50491da87f7 to your computer and use it in GitHub Desktop.
callbackにタイムアウトさせる
var fs = require('fs');
function timeoutify(fn, delay) {
var id = setTimeout(function () {
id = null;
fn(new Error('Timeout!'));
}, delay);
return function () {
if (id) {
clearTimeout(id);
fn.apply(this, arguments);
}
};
}
function display(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
}
fs.readFile('./timoutify.js', timeoutify(display, 500));
fs.readFile('./timoutify.js', timeoutify(display, 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment