Added adjusted interval & set errorFromCode to always return an error even on 200's
This commit is contained in:
parent
b292d5ed17
commit
db18c010aa
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.21.5",
|
"version": "0.21.6",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -180,7 +180,6 @@ export class GatewayTimeoutError extends CustomError {
|
|||||||
* @return {CustomError} The proper error type
|
* @return {CustomError} The proper error type
|
||||||
*/
|
*/
|
||||||
export function errorFromCode(code: number, message?: string) {
|
export function errorFromCode(code: number, message?: string) {
|
||||||
if(code >= 200 && code < 300) return null;
|
|
||||||
switch(code) {
|
switch(code) {
|
||||||
case 400:
|
case 400:
|
||||||
return new BadRequestError(message);
|
return new BadRequestError(message);
|
||||||
|
22
src/time.ts
22
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
|
* Return date formated highest to lowest: YYYY-MM-DD H:mm AM
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user