我正在尝试使用$ cacheFactory在$ resource之上实现异步缓存。
在$ http源中浏览它似乎支持从缓存返回promise。我假设在那之后,我可以简单地用缓存数据解决promise或拒绝它,然后让$ http做它的事情,然后将数据放回缓存中。问题是..我只是不工作。$ http实际上支持诺言吗?
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处理缓存的地方,如您所见,它确实检查了诺言是否正在返回(第898行)。但是看来,解决或拒绝都只是从挂起队列中清除请求。如何实际发送数据或单个$ http继续请求?
这是关于我要完成的工作的简要说明。
AngularJS中有一个错误。在解析(或拒绝)从缓存$ http的承诺返回时,不会以任何方式处理数据。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句