Skip to content

Instantly share code, notes, and snippets.

@geelen geelen/input.js
Created Dec 9, 2019

Embed
What would you like to do?
Proxy memo async
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))
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
You can’t perform that action at this time.