Created
June 24, 2025 12:14
-
-
Save checkmatez/dc4f61ede9409cabbb11b8afa6b2613d to your computer and use it in GitHub Desktop.
1
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
/* | |
Ensure that multiple calls to the calculateCheap function | |
with the same argument will not result in multiple | |
calls to the calculateExpensive function. | |
Expected console output: | |
calculateExpensive called with = 2 | |
result = 4 | |
result = 4 | |
result = 4 | |
*/ | |
async function calculateCheap(num: number): Promise<number> { | |
const result = await calculateExpensive(num); | |
return result; | |
} | |
/* | |
--------------------- | |
DO NOT EDIT CODE BELOW | |
---------------------- | |
*/ | |
/** | |
* Simulates an expensive calculation by doubling the input number after a delay. | |
*/ | |
async function calculateExpensive(num: number) { | |
console.log('calculateExpensive called with =', num); | |
return new Promise<number>(resolve => | |
setTimeout(() => resolve(num * 2), 500), | |
); | |
} | |
function logResult(result: number) { | |
console.log('result =', result); | |
} | |
calculateCheap(2).then(logResult); | |
calculateCheap(2).then(logResult); | |
calculateCheap(2).then(logResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment