diff --git a/package.json b/package.json index a883372..e8fdda5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.21.5", + "version": "0.21.6", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/errors.ts b/src/errors.ts index de47eb1..9780f57 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -180,7 +180,6 @@ export class GatewayTimeoutError extends CustomError { * @return {CustomError} The proper error type */ export function errorFromCode(code: number, message?: string) { - if(code >= 200 && code < 300) return null; switch(code) { case 400: return new BadRequestError(message); diff --git a/src/time.ts b/src/time.ts index 2f7188b..c32d7b3 100644 --- a/src/time.ts +++ b/src/time.ts @@ -1,3 +1,25 @@ +/** + * Like setInterval but will adjust the timeout value to account for runtime + * @param {Function} cb Callback function that will be ran + * @param {number} ms Run function ever x seconds + * @return {() => void} + */ +export function adjustedInterval(cb: Function, ms: number) { + let cancel = false, timeout: any = null; + const p = async () => { + if (cancel) return; + const start = new Date().getTime(); + await cb(); + const end = new Date().getTime(); + timeout = setTimeout(() => p(), ms - (end - start) || 1); + }; + p(); + return () => { + cancel = true; + if(timeout) clearTimeout(timeout); + } +} + /** * Return date formated highest to lowest: YYYY-MM-DD H:mm AM *