Created
December 9, 2019 15:07
-
-
Save geelen/ff28a6ed4db1658004f34e4374bc905e to your computer and use it in GitHub Desktop.
Proxy memo async
This file contains 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
const someExpensiveOperation = async x => { | |
await new Promise(resolve => setTimeout(resolve, 1000)) | |
return x.toUpperCase() | |
} | |
const PROXY_MEMO = Object.create( | |
new Proxy( | |
{}, | |
{ | |
get(_, handler) { | |
console.log(`called Proxy for '${handler}'`) | |
return (PROXY_MEMO[handler] = someExpensiveOperation(handler)) | |
} | |
} | |
) | |
) | |
async function test() { | |
return await Promise.all('abcabcabc'.split('').map(l => PROXY_MEMO[l])) | |
} | |
test().then(letters => console.log(letters)) |
This file contains 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
called Proxy for 'a' | |
called Proxy for 'b' | |
called Proxy for 'c' | |
Array(9) [ "A", "B", "C", "A", "B", "C", "A", "B", "C" ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment