UNPKG

async-kit

Version:

A simple and powerful async abstraction lib for easily writing Node.js code.

66 lines (43 loc) 1.91 kB
/* Async Kit Copyright (c) 2014 - 2016 Cédric Ronvel The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ "use strict" ; var wrapper = {} ; module.exports = wrapper ; // Maybe I should have a look to the 'wrappy' package from npm wrapper.timeout = function timeout( fn , timeout_ , fnThis ) { var fnWrapper = function() { var this_ = fnThis || this , alreadyCalledBack = false , args = Array.prototype.slice.call( arguments ) , callback = args.pop() ; var callbackWrapper = function() { if ( alreadyCalledBack ) { return ; } alreadyCalledBack = true ; callback.apply( this_ , arguments ) ; } ; args.push( callbackWrapper ) ; fn.apply( this_ , args ) ; setTimeout( callbackWrapper.bind( undefined , new Error( 'Timeout' ) ) , timeout_ ) ; } ; // Should we copy own properties of fn into fnWrapper? return fnWrapper ; } ;