This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}) | |
]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (this.mutexDataMap.has(key)) { | |
this.log(key + 'InsideMutex: Data is already loaded'); | |
return true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}) | |
]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}) | |
]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |