{"action":"opened","issue":{"url":"https://api.github.com/repos/getify/You-Dont-Know-JS/issues/381","labels_url":"https://api.github.com/repos/getify/You-Dont-Know-JS/issues/381/labels{/name}","comments_url":"https://api.github.com/repos/getify/You-Dont-Know-JS/issues/381/comments","events_url":"https://api.github.com/repos/getify/You-Dont-Know-JS/issues/381/events","html_url":"https://github.com/getify/You-Dont-Know-JS/issues/381","id":60612388,"number":381,"title":"[Q] [async & performance] Trying to save callbacks example","user":{"login":"babygau","id":1514823,"avatar_url":"https://avatars.githubusercontent.com/u/1514823?v=3","gravatar_id":"","url":"https://api.github.com/users/babygau","html_url":"https://github.com/babygau","followers_url":"https://api.github.com/users/babygau/followers","following_url":"https://api.github.com/users/babygau/following{/other_user}","gists_url":"https://api.github.com/users/babygau/gists{/gist_id}","starred_url":"https://api.github.com/users/babygau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/babygau/subscriptions","organizations_url":"https://api.github.com/users/babygau/orgs","repos_url":"https://api.github.com/users/babygau/repos","events_url":"https://api.github.com/users/babygau/events{/privacy}","received_events_url":"https://api.github.com/users/babygau/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-03-11T05:26:26Z","updated_at":"2015-03-11T05:26:26Z","closed_at":null,"body":"Hi,\r\n\r\nI am trying to get my head round the `timeoutify()` function which is used to solve `the trust issue of never being called` \r\n\r\n> What about the trust issue of never being called? If this is a concern (and it probably should be!), you likely will need to set up a timeout that cancels the event. You could make a utility (proof-of-concept only shown) to help you with that:\r\n\r\n```js\r\nfunction timeoutify(fn,delay) {\r\n\tvar intv = setTimeout( function(){\r\n\t\t\tintv = null;\r\n\t\t\tfn( new Error( \"Timeout!\" ) );\r\n\t\t}, delay )\r\n\t;\r\n\r\n\treturn function() {\r\n\t\t// timeout hasn't happened yet?\r\n\t\tif (intv) {\r\n\t\t\tclearTimeout( intv );\r\n\t\t\tfn.apply( this, arguments );\r\n\t\t}\r\n\t};\r\n}\r\n\r\n// using \"error-first style\" callback design\r\nfunction foo(err,data) {\r\n\tif (err) {\r\n\t\tconsole.error( err );\r\n\t}\r\n\telse {\r\n\t\tconsole.log( data );\r\n\t}\r\n}\r\n\r\najax( \"http://some.url.1\", timeoutify( foo, 500 ) );\r\n```\r\nFrom my understanding, `fn` is required to be executed once within `500ms`, if it is fired after that given  time, an `err` obj would be passed on `fn` callback. But I recalled in chapter 1, `A JavaScript program is (practically) always broken up into two or more chunks, where the first chunk runs *now* and the next chunk runs *later*, in response to an event`. I would describe the `timeoutify` function as the following\r\n\r\n```javascript\r\n// now\r\nfunction timeoutify(fn, delay) {\r\n  ...\r\n  return function {\r\n    // timeout hasn't happened yet?\r\n    if (intv) {\r\n      clearTimeout( intv );\r\n      fn.apply( this, arguments );\r\n    }\r\n  }\r\n}\r\n\r\n// later\r\nvar intv = setTimeout( function(){\r\n\t\t\tintv = null;\r\n\t\t\tfn( new Error( \"Timeout!\" ) );\r\n\t\t}, delay );\r\n```\r\n\r\nAs the result of that, `fn( new Error( \"Timeout!\" ) )` is never called because of `clearTimeout` function. Please correct my mistake and thank you for reading!"}}