result: 549
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "set cache value failed.".] {
code: 'ERR_UNHANDLED_REJECTION'
}
When using an async cache and the 'set' operation throws an error we get "unhandled rejection".
` import { AsyncCache, memoizeAsync } from 'utils-decorators';
class DistributedCache<T = any> implements AsyncCache {
private cache = {};
async set(key: string, value: T): Promise {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('set cache value failed.');
}, 1000);
});
}
async get(key: string): Promise {
return await this.cache[key];
}
async delete(key: string): Promise {
delete this.cache[key];
}
async has(key: string): Promise {
const val = await this.get(key);
} }
class Test { @memoizeAsync({ cache: new DistributedCache(), }) getRand(): Promise {
return new Promise((resolve) => {
setTimeout(() => {
resolve(Math.floor(Math.random() * 1000));
}, 1000);
});
}
}
const test = new Test();
test .getRand() .then((res) => console.log(
result: ${res}
)) .catch((err) => console.log(error: ${err}
)); `console output before the fix:
result: 549 [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "set cache value failed.".] { code: 'ERR_UNHANDLED_REJECTION' }
console output after the fix:
error: set cache value failed.