Last active
October 27, 2015 23:29
-
-
Save taka011239/ee38f58df50491da87f7 to your computer and use it in GitHub Desktop.
callbackにタイムアウトさせる
This file contains hidden or 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
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