问题是getMasterData,当被调用时从不打印“通过最严重的障碍” :(。上帝知道我做错了。这是我真正想要实现的目标。
我在这里做错了什么? 请告诉我。任何帮助将不胜感激。
以下是我的节点redis实现的实现:
如果我在循环中运行相同的this.getAll()而没有任何等待,它将完美地工作。
第二次等待它永远不会从this.cacheStore.getByKey('organizations')返回,并永远陷入死锁。
没有错误,也没有响应。
如果我删除data2也要等待。尽管如此,它仍然可以正常工作。
async setKey(key, data) {
try {
const flatteredData = JSON.stringify(data);
return this.cacheStore.hmset(key, key, flatteredData);
} catch (error) {
return error;
}
}
getByKey(key) { // eslint-disable-line consistent-return
return new Promise((resolve, reject) => {
this.cacheStore.hget(key, key, (err, res) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(res));
}
});
});
}
async getAll() {
const cache = await this.cacheStore.getByKey('organizations');
if (cache) return cache;
const organizations = await this.db
.collection('organizations')
.find({})
.toArray();
await this.cacheStore.setKey('organizations', organizations);
return organizations;
}
async getMasterData(){
const data1 = await this.getAll();
const data2 = await this.getAll();
console.log('Passed the worst barrier');
}
答案 0 :(得分:0)
您将setKey()
声明为async
函数,但是在getAll()
中调用它时,却忘记了await
。
删除async
或用await
调用它。我不确定这是否可以解决您的错误,请尝试一下!