Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save checkmatez/dc4f61ede9409cabbb11b8afa6b2613d to your computer and use it in GitHub Desktop.
Save checkmatez/dc4f61ede9409cabbb11b8afa6b2613d to your computer and use it in GitHub Desktop.
1
/*
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