我有以下代码。我将一些数据保存到并行回调中的缓存变量中,但是在并行内部,缓存对象始终为空。有任何想法吗?
Topics.getTopicsByTids = function(tids, uid, callback) {
var cache = {};
function loadTopicInfo(topicData, next) {
async.parallel({
privileges: function(next) {
console.log(cache); // always prints empty object
if (cache[topicData.cid]) {
console.log('CACHE');
return next(null, cache[topicData.cid])
}
categoryTools.privileges(topicData.cid, uid, next);
}
}, function(err, topicInfo) {
// save privs to cache, doesnt seem to modify
//the cache object in the outer scope
cache[topicData.cid] = topicInfo.privileges;
console.log(cache); // prints out the cached data
next(null, topicData);
});
}
Topics.getTopicsData(tids, function(err, topics) {
async.map(topics, loadTopicInfo, callback);
});
};
问题在于async.map
它同时要求loadTopicInfo
20个主题。因此,在将任何内容保存到缓存之前就进行了缓存检查。H!用async.eachSeries替换它可以解决此问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句