Created
September 28, 2024 12:33
-
-
Save alaindet/b898d2d04e7f2edfe1a44f23c282c75e to your computer and use it in GitHub Desktop.
Cached Promises
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
// Thanks to | |
// https://github.com/microsoft/tsyringe/issues/66#issuecomment-566755746 | |
// https://262.ecma-international.org/6.0/#sec-promise-resolve-functions | |
main(); | |
async function main() { | |
const rnd = getRandom(); | |
console.log(await rnd); | |
console.log(await rnd); | |
console.log(await rnd); | |
} | |
async function getRandom() { | |
return Promise.resolve(Math.random()); | |
} | |
// Outputs | |
// 0.2904893310271659 | |
// 0.2904893310271659 | |
// 0.2904893310271659 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment