GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add real-life example · HowProgrammingWorks/Errors@4886aac

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • JavaScript
    • 9-retry.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎JavaScript/9-retry.js‎

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const DEFAULT_RETRY = 3;
4+
5+
const httpGet = async ({ url, retry = DEFAULT_RETRY }) => {
6+
console.log({ url, retry });
7+
const res = await fetch(url).catch(() => ({ ok: false }));
8+
if (!res.ok) {
9+
const attemptsLeft = retry - 1;
10+
if (attemptsLeft > 0) return httpGet({ url, retry: attemptsLeft });
11+
throw new Error('Can not get data');
12+
}
13+
return res.json();
14+
};
15+
16+
const main = async () => {
17+
try {
18+
const key = '1f43ea96b1e343fe94333dd2b97a109d';
19+
const url = 'https://openexchangerates.org/api/latest.json?app_id=' + key;
20+
const data = await httpGet({ url });
21+
const rate = data.rates['UAH'];
22+
console.log({ rate });
23+
} catch (err) {
24+
console.error({ err });
25+
}
26+
};
27+
28+
main();

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.