Skip to content

Instantly share code, notes, and snippets.

@lorenzofox3
Created June 23, 2020 10:41
Show Gist options
  • Save lorenzofox3/10c617846bb88a64842dd7a73a5928d0 to your computer and use it in GitHub Desktop.
Save lorenzofox3/10c617846bb88a64842dd7a73a5928d0 to your computer and use it in GitHub Desktop.
check whether lodash memoize works on async function
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