Skip to content

Commit 8178651

Browse files
committed
Improve examples
1 parent 64153ce commit 8178651

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

JavaScript/async-await/files.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22

3-
import fs from 'fs';
4-
import promisify from '../promisify';
3+
const fs = require('fs');
4+
const { promisify } = require('util');
55

66
const readFile = promisify(fs.readFile);
77

8-
async function readTextFile(filename) {
8+
const readTextFile = async (filename) => {
99
let data = await readFile(filename);
1010
return data.toString();
11-
}
11+
};
1212

1313
(async () => {
1414
console.log(await readTextFile('../file1.txt'));

JavaScript/async-await/http-client.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
import httpGet from '../get-json';
4-
3+
const { httpGet } = require('../get-json');
54
const baseUrl = 'http://localhost:3000/';
65

76
(async () => {

JavaScript/d-await.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
const httpGet = require('./get-json');
44

55
const baseUrl = 'http://localhost:3000/';
6+
67
(async () => {
7-
const api = await httpGet(baseUrl);
8+
try {
9+
const api = await httpGet(baseUrl);
10+
} catch (err) {
11+
console.log(err.code);
12+
}
813
for (const resource of api.resources) {
914
const data = await httpGet(baseUrl + resource);
1015
console.log(data);

JavaScript/get-json.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const http = require('http');
44

5-
module.exports = url => new Promise((resolve, reject) => {
5+
const httpGet = url => new Promise((resolve, reject) => {
66
http.get(url, res => {
77
const code = res.statusCode;
88
if (code !== 200) {
@@ -30,3 +30,5 @@ module.exports = url => new Promise((resolve, reject) => {
3030
});
3131
});
3232
});
33+
34+
module.exports = { httpGet };

0 commit comments

Comments
 (0)