param { AsyncFunction } fn param { number } expirationTime param { string } errorMessage returns export function timeout < T extends Function > ( fn , expirationTime , errorMessage ? ) { errorMessage = errorMessage || `Could not resolve ${ fn . name || '<anonymous>' } within ${ expirationTime } ms` ; return ( async function race ( this ) { return Promise . race ( [ new Promise ( ( __ , reject ) : void => { setTimeout ( ( ) : void => reject ( new Error ( errorMessage ) ) , expirationTime ) ; } ) as Promise < never > , fn . apply ( this , arguments ) , ] ) ; } as any ) as T ; }