Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
val: string = "";
constructor(id: string, id2: string) {
this.id = id;
}
async refresh(): Promise {
return new Promise(resolve => {
setTimeout(() => {
this.val = "done";
resolve("done");
}, 1000);
});
}
}
const myCache = new AsyncCache<{ id: string }, MyClass>((obj: { id: string; }) => {
return Cache.hash([obj.id]);
});
expect(myCache.has({ id: "007" })).is.false;
let callbackCount = 0;
const promises = [];
for (let i = 0; i < 10; ++i) {
promises.push(myCache.get({ id: "007" }, async () => {
const retVal = new MyClass("007", "a");
await retVal.refresh();
++callbackCount;
return retVal;
}));
}
expect(callbackCount).to.equal(0);
expect(myCache.has({ id: "007" })).is.true;
expect(myCache.has({ id: "008" })).is.false;