-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2-fetch.js
More file actions
20 lines (16 loc) · 470 Bytes
/
Copy path2-fetch.js
File metadata and controls
20 lines (16 loc) · 470 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'use strict';
const http = require('http');
const fetch = url => new Promise((resolve, reject) => http.get(url, res => {
if (res.statusCode !== 200) {
reject(`Status Code: ${res.statusCode}`);
return;
}
res.setEncoding('utf8');
const lines = [];
res.on('data', chunk => lines.push(chunk));
res.on('end', () => resolve(lines.join()));
}));
// Usage
fetch('http://ietf.org/')
.then(body => console.log(body))
.catch(err => console.error(err));