-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
27 lines (24 loc) · 814 Bytes
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// @ts-check
import assert from 'node:assert';
import { test } from 'node:test';
import createCachedPromiseGetter from "./index.js";
test('Simplest use case', async () => {
const timeout = setTimeout(() => {
console.log('timeout');
}, 2000);
/**
* @typedef {object} MyData
* @property {string} someProp
*/
const cache = (func) => {
let cachedValue;
const funcWithCache = (...args) => cachedValue ? cachedValue : (cachedValue = func(...args));
return funcWithCache;
}
const myDataPromiseGetter = /** @type {typeof createCachedPromiseGetter<MyData>} */(createCachedPromiseGetter)(cache);
const obj = {someProp: "some value"};
myDataPromiseGetter().resolve(obj);
const myData = await myDataPromiseGetter();
assert.strictEqual(myData, obj);
clearTimeout(timeout);
});