Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { timeoutPromise } from './timeoutPromise';
  2. /**
  3. * The number of milliseconds before deciding that we need retry a fetch request.
  4. *
  5. * @type {number}
  6. */
  7. const RETRY_TIMEOUT = 3000;
  8. /**
  9. * Wrapper around fetch GET requests to handle json-ifying the response
  10. * and logging errors.
  11. *
  12. * @param {string} url - The URL to perform a GET against.
  13. * @param {?boolean} retry - Whether the request will be retried after short timeout.
  14. * @param {?Object} options - The request options.
  15. * @returns {Promise<Object>} The response body, in JSON format, will be
  16. * through the Promise.
  17. */
  18. export function doGetJSON(url, retry, options) {
  19. const fetchPromise = fetch(url, options)
  20. .then(response => {
  21. const jsonify = response.json();
  22. if (response.ok) {
  23. return jsonify;
  24. }
  25. return jsonify
  26. .then(result => Promise.reject(result));
  27. });
  28. if (retry) {
  29. return timeoutPromise(fetchPromise, RETRY_TIMEOUT)
  30. .catch(response => {
  31. if (response.status >= 400 && response.status < 500) {
  32. return Promise.reject(response);
  33. }
  34. return timeoutPromise(fetchPromise, RETRY_TIMEOUT);
  35. });
  36. }
  37. return fetchPromise;
  38. }