Skip to content

Instantly share code, notes, and snippets.

View dontpaniclabsgists's full-sized avatar

Don't Panic Labs dontpaniclabsgists

View GitHub Profile
loadWithMutexLockNoDataCheck = async (key: string): Promise<boolean> => {
this.log(key + 'Calling loadWithMutexLock No Data Check');
return this.mutex.runExclusive(async () => {
this.log(key + '***START*** Loading data');
await this.slowApiCall(key);
this.mutexDataMap.set(key, 'data');
this.log(key + '***END*** Data loaded');
return true;
private initialize_Mutex = async () => {
this.startTime = new Date().getTime();
await Promise.all([
...Array.from({ length: 3 }, async () => {
var key = 'mutexKey ';
await this.loadWithMutexLock(key);
this.log(this.mutexDataMap);
})
]);
}
if (this.mutexDataMap.has(key)) {
this.log(key + 'InsideMutex: Data is already loaded');
return true;
}
import { Mutex } from 'async-mutex';
private mutex = new Mutex();
loadWithMutexLock = async (key: string): Promise<boolean> => {
this.log(key + 'Calling loadWithMutexLock');
const random = Math.random();
this.log(key + 'Random: ' + random);
// Check if the data is already loaded
if (this.mutexDataMap.has(key)) {
this.log(key + 'Data is already loaded');
private initialize_Promise = async () => {
this.startTime = new Date().getTime();
await Promise.all([
...Array.from({ length: 3 }, async () => {
var key = 'promiseKey ';
await this.loadWithPromiseLock(key);
this.log(this.promiseDataMap);
})
]);
}
loadWithPromiseLock = async (key: string): Promise<boolean> => {
this.log(key + 'Calling loadWithPromiseLock');
const random = Math.random();
this.log(key + 'Random: ' + random);
// Check if the data is already loaded
if (this.promiseDataMap.has(key)) {
this.log(key + 'Data is already loaded');
return true;
}
private initialize_Promise_NoCheck = async() => {
this.startTime = new Date().getTime();
await Promise.all([
...Array.from({ length: 3 }, async () => {
var key = 'promiseKey_NoDataCheck ';
await this.loadWithPromiseLockNoDataCheck(key);
this.log(this.promiseDataMap);
})
]);
}
loadWithPromiseLockNoDataCheck = async (key: string): Promise<boolean> => {
this.log(key + 'Calling loadWithPromiseLock No Data Check');
const random = Math.random();
this.log(key + 'Random: ' + random);
// Create a new loading promise
return (async () => {
this.log(key + '***START*** Loading data');
await this.slowApiCall(key);
let numbers: number[] = [85, 90, 78, 92, 88];
let total = 0;
for (let index in numbers) {
total += numbers[index];
}
let average = total / numbers.length;
console.log("Average:", average); // Output: 86.6
let numbers: number[] = [85, 90, 78, 92, 88];
let total = 0;
for (let number of numbers) {
total += number;
}
let average = total / numbers.length;
console.log("Average:", average); // Output: 86.6