We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 860a9b8 commit 590609fCopy full SHA for 590609f
JavaScript/2-fetch.js
JavaScript/3-fetch.js
@@ -0,0 +1,24 @@
1
+'use strict';
2
+
3
+const http = require('http');
4
+const https = require('https');
5
6
+const fetch = url => new Promise((resolve, reject) => {
7
+ const protocol = url.startsWith('https') ? https : http;
8
+ protocol.get(url, res => {
9
+ if (res.statusCode !== 200) {
10
+ const { statusCode, statusMessage } = res;
11
+ reject(new Error(`Status Code: ${statusCode} ${statusMessage}`));
12
+ }
13
+ res.setEncoding('utf8');
14
+ const lines = [];
15
+ res.on('data', chunk => lines.push(chunk));
16
+ res.on('end', () => resolve(lines.join()));
17
+ });
18
+});
19
20
+// Usage
21
22
+fetch('http://ietf.org/')
23
+ .then(body => console.log(body))
24
+ .catch(err => console.error(err));
0 commit comments