Skip to main content
Loading...

More JavaScript Posts

//Retries maxRetries number of times, meaning that if you have 0, then it'll run the function once.
async function retryPromise(promiseFn, maxRetries = 3, delayOffset = 0, delayRandomRange = 0) {
	return new Promise((resolve, reject) => {
		promiseFn()
			.then(resolve, (error) => { //On error
				if (maxRetries <= 0) {
					return reject(error);
				} else {
					if(delayRandomRange * Math.random() + delayOffset < 1.0){
						return retryPromise(promiseFn, maxRetries - 1, delayOffset, delayRandomRange).then(resolve, reject);
					}else{
						return new Promise((resolveTwo, rejectTwo) => {
							setTimeout(() => {
								return retryPromise(promiseFn, maxRetries - 1, delayOffset, delayRandomRange).then(resolveTwo, rejectTwo);
							}, delayRandomRange * Math.random() + delayOffset);
						}).then(resolve, reject);
					}
				}
			});
	});
}

//Tests for that function.

//Returns a function that will fail numTimes times, then will return aValue
function functionThatReturnsAFunctionThatFailsACoupleTimes(numTimes, aValue){
	return async function(){
		if(numTimes == 0){
			return aValue;
		}
		numTimes--;
		throw false;
	};
}

const SMALL_NUMBER = 10;

function testTest(failNumberOfTimes){

	let functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(failNumberOfTimes, true);

	//Test my test
	for(let i = 0; i < (failNumberOfTimes * 2); ++i){
		function evalTest(val){
			let testTestFailedReason = "";
			if(val === undefined){
				testTestFailedReason = "Test test returned undefined for some reason.";
			}
			if((i + 1) > failNumberOfTimes){
				if(val === true){
					//We're good
				}else{
					testTestFailedReason = "Test test didn't return true when it should have.";
				}
			}else{
				if(val === false){
					//We're good
				}else{
					testTestFailedReason = "Test test didn't return false when it should have.";
				}
			}
			testTestFailedReason = testTestFailedReason || "Test test passed test case";
			console.log(testTestFailedReason, "at index", i, "where the function returned", val);
		}
		functionThatFailsACoupleTimes().then(evalTest, evalTest)
	};
}

testTest(SMALL_NUMBER);

let testCaseCounter = 1;

const throwsNoError = [
	(val)=>{
		if(val == true){
			console.log("Passed test case " + (testCaseCounter++));
		}else{
			console.error("Unexpected return value", val);
		}
	},
	()=>{
		console.error("It wasn't supposed to fail!")
	}
]

const throwsError = [
	(val)=>{
		console.error("It wasn't supposed to succeed!", val);
	},
	(val)=>{
		if(val == false){
			console.log("Passed test case " + (testCaseCounter++));
		}else{
			console.error("Unexpected return value", val);
		}
	}
];

//Runs SMALL_NUMBER times, because SMALL_NUMBER - 1 is the number of retries after the first one
let functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);

retryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 1).then(...throwsNoError)

functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);

//Runs SMALL_NUMBER - 1 times, because SMALL_NUMBER - 2 is the number of retries after the first one
retryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 2).then(...throwsError)

//Testing the delay. You'll have to wait a bit too.

functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);

//Runs SMALL_NUMBER times, because SMALL_NUMBER - 1 is the number of retries after the first one
retryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 1, 500, 500).then(...throwsNoError)

functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);

//Runs SMALL_NUMBER - 1 times, because SMALL_NUMBER - 2 is the number of retries after the first one
retryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 2, 500, 500).then(...throwsError)
var arr = [{
    "success": true,
    "data": [
        {
            "id": "ipi_1KrrbvDOiB2klwsKhuqUWqt1",
            "object": "issuing.transaction",
            "amount": -2743,
            "amount_details": {
                "atm_fee": null
            },
            "authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd",
            "balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi",
            "card": "ic_1Krqe5DOiB2klwsK44a35eiE",
            "cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr",
            "created": 1650753567,
            "currency": "usd",
            "dispute": null,
            "livemode": false,
            "merchant_amount": -2743,
            "merchant_currency": "usd",
            "merchant_data": {
                "category": "advertising_services",
                "category_code": "7311",
                "city": "San Francisco",
                "country": "US",
                "name": "Aeros Marketing, LLC",
                "network_id": "1234567890",
                "postal_code": "94103",
                "state": "CA"
            },
            "metadata": {},
            "type": "capture",
            "wallet": null
        },
                {
            "id": "ipi_1Krrbvc62B2klwsKhuqUWqt1",
            "object": "issuing.transaction",
            "amount": -9999,
            "amount_details": {
                "atm_fee": null
            },
            "authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd",
            "balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi",
            "card": "ic_1Krqe5DOiB2klwsK44a35eiE",
            "cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr",
            "created": 1650753567,
            "currency": "USD",
            "dispute": null,
            "livemode": false,
            "merchant_amount": -9999,
            "merchant_currency": "usd",
            "merchant_data": {
                "category": "fast_food",
                "category_code": "7311",
                "city": "San Francisco",
                "country": "US",
                "name": "Aeros Marketing, LLC",
                "network_id": "1234567890",
                "postal_code": "94103",
                "state": "CA"
            },
            "metadata": {},
            "type": "capture",
            "wallet": null
        }
    ]
}];
    

const reduced = arr.reduce((prev, curr) => {
    prev.push({
        amount: curr.data[0].merchant_amount,
        id: curr.data[0].id,
        currency: curr.data[0].currency,
        category: curr.data[0].merchant_data.category
    });
    console.log(prev)

    return prev;
    
}, []);