我正在尝试使用 $cacheFactory 在 $resource 之上实现异步缓存。
在 $http 源码中查找它似乎支持从缓存中返回 promise 。我会假设在那之后我可以简单地用缓存数据解决 promise 或拒绝它并让 $http 做它的事情,然后将数据放回缓存中。问题是.. 我只是不工作。 $http 实际上支持 promises 吗?
https://github.com/angular/angular.js/blob/master/src/ng/http.js#L895
if (cache) {
cachedResp = cache.get(url);
if (isDefined(cachedResp)) {
if (isPromiseLike(cachedResp)) {
// cached request has already been sent, but there is no response yet
cachedResp.then(removePendingReq, removePendingReq);
return cachedResp;
} else {
// serving from cache
if (isArray(cachedResp)) {
resolvePromise(cachedResp[1], cachedResp[0], shallowCopy(cachedResp[2]), cachedResp[3]);
} else {
resolvePromise(cachedResp, 200, {}, 'OK');
}
}
} else {
// put the promise for the non-transformed response into cache as a placeholder
cache.put(url, promise);
}
}
这是 $http 处理缓存的地方,正如您所看到的,它确实检查了 promise 是否正在返回(第 898 行)。但似乎解决或拒绝都只是从待处理队列中清除请求。我如何实际发送数据或单个 $http 以继续请求?
这是关于我正在努力完成的事情的 Plunker。
最佳答案
AngularJS 中有一个错误。当解析(或拒绝)来自缓存的 promise 返回时,$http 不会以任何方式处理数据。
关于javascript - 异步 $cacheFactory 与 $http 和 $resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24920900/