Created
June 23, 2020 10:41
-
-
Save lorenzofox3/10c617846bb88a64842dd7a73a5928d0 to your computer and use it in GitHub Desktop.
check whether lodash memoize works on async function
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 {test} = require(`zora`); | |
const memoize = require(`lodash/memoize.js`); | |
let callCount = 0; | |
const wait = (time = 200) => new Promise((resolve) => { | |
setTimeout(() => resolve(), time); | |
}); | |
const fetchRemoteResource = memoize(async () => { | |
callCount++; | |
await wait(); | |
return 'whatever'; | |
}); | |
test(`should only be called once`, async t => { | |
const res1 = await fetchRemoteResource(); | |
const res2 = await fetchRemoteResource(); | |
t.eq(res1, 'whatever'); | |
t.eq(res2, 'whatever'); | |
t.eq(callCount, 1); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment