Skip to main content
Loading...

More JavaScript Posts

import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'


const { exec } = require("child_process");

exec("sensors|grep "high"|grep "Core"|cut -d "+" -f2|cut -d "." -f1|sort -nr|sed -n 1p", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

export default function Home() {
  return (
    <div className={styles.container}>
      <Head>
        <title>Create Next App</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <main className={styles.main}>
        <h1 className={styles.title}>
          Welcome to <a href="https://nextjs.org">Next.js!</a>
        </h1>

        <p className={styles.description}>
          Get started by editing{' '}
          <code className={styles.code}>pages/index.js</code>
        </p>

        <div className={styles.grid}>
          <a href="https://nextjs.org/docs" className={styles.card}>
            <h2>Documentation &rarr;</h2>
            <p>Find in-depth information about Next.js features and API.</p>
          </a>

          <a href="https://nextjs.org/learn" className={styles.card}>
            <h2>Learn &rarr;</h2>
            <p>Learn about Next.js in an interactive course with quizzes!</p>
          </a>

          <a
            href="https://github.com/vercel/next.js/tree/canary/examples"
            className={styles.card}
          >
            <h2>Examples &rarr;</h2>
            <p>Discover and deploy boilerplate example Next.js projects.</p>
          </a>

          <a
            href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
            className={styles.card}
          >
            <h2>Deploy &rarr;</h2>
            <p>
              Instantly deploy your Next.js site to a public URL with Vercel.
            </p>
          </a>
        </div>
      </main>

      <footer className={styles.footer}>
        <a
          href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
          target="_blank"
          rel="noopener noreferrer"
        >
          Powered by{' '}
          <span className={styles.logo}>
            <Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
          </span>
        </a>
      </footer>
    </div>
  )
}
//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)