Added adjusted interval & set errorFromCode to always return an error even on 200's
All checks were successful
Build / Build NPM Project (push) Successful in 1m16s
Build / Tag Version (push) Successful in 14s
Build / Publish Documentation (push) Successful in 2m1s

This commit is contained in:
Zakary Timson 2024-10-29 10:41:25 -04:00
parent b292d5ed17
commit db18c010aa
3 changed files with 23 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.21.5",
"version": "0.21.6",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -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);

View File

@ -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
*