Skip to content

Commit 590609f

Browse files
committed
Improve fetch to use https and reorder examples
1 parent 860a9b8 commit 590609f

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

JavaScript/2-fetch.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

JavaScript/3-fetch.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy