UI for Zipcoin Blue

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2014 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. 'use strict';
  14. var helpers = require('../helpers');
  15. var cacheOnly = require('./cacheOnly');
  16. function fastest(request, values, options) {
  17. helpers.debug('Strategy: fastest [' + request.url + ']', options);
  18. return new Promise(function(resolve, reject) {
  19. var rejected = false;
  20. var reasons = [];
  21. var maybeReject = function(reason) {
  22. reasons.push(reason.toString());
  23. if (rejected) {
  24. reject(new Error('Both cache and network failed: "' +
  25. reasons.join('", "') + '"'));
  26. } else {
  27. rejected = true;
  28. }
  29. };
  30. var maybeResolve = function(result) {
  31. if (result instanceof Response) {
  32. resolve(result);
  33. } else {
  34. maybeReject('No result returned');
  35. }
  36. };
  37. helpers.fetchAndCache(request.clone(), options)
  38. .then(maybeResolve, maybeReject);
  39. cacheOnly(request, values, options)
  40. .then(maybeResolve, maybeReject);
  41. });
  42. }
  43. module.exports = fastest;