Skip to content

Commit 7af0f45

Browse files
committed
Code style
1 parent 9f7b199 commit 7af0f45

9 files changed

Lines changed: 116 additions & 114 deletions

File tree

JavaScript/2-promises.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

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

5-
function readFile(filename) {
6-
return new Promise((resolve, reject) => {
7-
fs.readFile(filename, (err, data) => {
8-
if (err) {
9-
reject(err);
10-
} else {
11-
resolve(data.toString());
12-
}
13-
});
5+
const readFile = (filename) => new Promise((resolve, reject) => {
6+
fs.readFile(filename, (err, data) => {
7+
if (err) reject(err);
8+
else resolve(data.toString());
149
});
15-
}
16-
17-
readFile('file1.txt').then((data) => {
18-
console.log(data);
19-
return readFile('file2.txt');
20-
}).then((data) => {
21-
console.log(data);
22-
return readFile('file3.txt');
23-
}).then((data) => {
24-
console.log(data);
2510
});
11+
12+
readFile('file1.txt')
13+
.then(data => {
14+
console.log(data);
15+
return readFile('file2.txt');
16+
})
17+
.then(data => {
18+
console.log(data);
19+
return readFile('file3.txt');
20+
})
21+
.then(data => {
22+
console.log(data);
23+
});

JavaScript/3-promises.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ const promisify = require('./promisify');
55

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

8-
readFile('file1.txt').then((data) => {
9-
console.log(data.toString());
10-
return readFile('file2.txt');
11-
}).then((data) => {
12-
console.log(data.toString());
13-
return readFile('file3.txt');
14-
}).then((data) => {
15-
console.log(data.toString());
16-
});
8+
readFile('file1.txt')
9+
.then(data => {
10+
console.log(data.toString());
11+
return readFile('file2.txt');
12+
})
13+
.then(data => {
14+
console.log(data.toString());
15+
return readFile('file3.txt');
16+
})
17+
.then(data => {
18+
console.log(data.toString());
19+
});

JavaScript/4-promises.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ const promisify = require('./promisify');
55

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

8-
function readTextFile(filename) {
9-
const promise = readFile(filename).then(data => data.toString());
10-
return promise;
11-
}
8+
const readTextFile = filename => readFile(filename)
9+
.then(data => data.toString());
1210

13-
readTextFile('file1.txt').then((data) => {
14-
console.log(data);
15-
return readTextFile('file2.txt');
16-
}).then((data) => {
17-
console.log(data);
18-
return readTextFile('file3.txt');
19-
}).then((data) => {
20-
console.log(data);
21-
});
11+
readTextFile('file1.txt')
12+
.then((data) => {
13+
console.log(data);
14+
return readTextFile('file2.txt');
15+
})
16+
.then((data) => {
17+
console.log(data);
18+
return readTextFile('file3.txt');
19+
})
20+
.then((data) => {
21+
console.log(data);
22+
});

JavaScript/5-catch.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ const promisify = require('./promisify');
55

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

8-
function readTextFile(filename) {
9-
const promise = readFile(filename).then(data => data.toString());
10-
return promise;
11-
}
8+
const readTextFile = filename => readFile(filename)
9+
.then(data => data.toString());
1210

13-
readTextFile('file1.txt').then((data) => {
14-
console.log(data);
15-
return readTextFile('file2.txt');
16-
}).then((data) => {
17-
console.log(data);
18-
return readTextFile('file3-!!!.txt');
19-
}).then((data) => {
20-
console.log(data);
21-
}).catch((reason) => {
22-
console.log('Cannot read file');
23-
console.log(reason);
24-
});
11+
readTextFile('file1.txt')
12+
.then((data) => {
13+
console.log(data);
14+
return readTextFile('file2.txt');
15+
})
16+
.then((data) => {
17+
console.log(data);
18+
return readTextFile('file3-!!!.txt');
19+
})
20+
.then((data) => {
21+
console.log(data);
22+
})
23+
.catch((reason) => {
24+
console.log('Cannot read file');
25+
console.log(reason);
26+
});

JavaScript/6-onReject.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ const promisify = require('./promisify');
55

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

8-
function readTextFile(filename) {
9-
const promise = readFile(filename).then(data => data.toString());
10-
return promise;
11-
}
8+
const readTextFile = filename => readFile(filename)
9+
.then(data => data.toString());
1210

13-
readTextFile('file1.txt').then((data) => {
14-
console.log(data);
15-
return readTextFile('file2-!!!.txt');
16-
}).then((data) => {
17-
console.log(data);
18-
return readTextFile('file3.txt');
19-
}, (reason) => {
20-
console.log('Cannot read file');
21-
console.log(reason);
22-
return readTextFile('file3.txt');
23-
}).then((data) => {
24-
console.log(data);
25-
});
11+
readTextFile('file1.txt')
12+
.then((data) => {
13+
console.log(data);
14+
return readTextFile('file2-!!!.txt');
15+
})
16+
.then((data) => {
17+
console.log(data);
18+
return readTextFile('file3.txt');
19+
}, (reason) => {
20+
console.log('Cannot read file');
21+
console.log(reason);
22+
return readTextFile('file3.txt');
23+
})
24+
.then((data) => {
25+
console.log(data);
26+
});

JavaScript/7-http.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
const httpGet = require('./get-json');
44

55
const baseUrl = 'http://localhost:3000/';
6-
httpGet(baseUrl + 'person').then((data) => {
7-
console.log(data);
8-
return httpGet(baseUrl + 'city');
9-
}).then(console.log);
6+
7+
httpGet(baseUrl + 'person')
8+
.then((data) => {
9+
console.log(data);
10+
return httpGet(baseUrl + 'city');
11+
})
12+
.then(console.log);

JavaScript/8-http.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const httpGet = require('./get-json');
44

55
const baseUrl = 'http://localhost:3000/';
6+
67
httpGet(baseUrl).then((api) => {
78
const promises = [];
89
for (const resource of api.resources) {

JavaScript/get-json.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,31 @@
22

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

5-
module.exports = (url) => {
6-
return new Promise((resolve, reject) => {
7-
http.get(url, res => {
8-
const code = res.statusCode;
9-
if (code !== 200) {
10-
return reject(new Error(`HTTP status code ${code}`));
11-
}
5+
module.exports = (url) => new Promise((resolve, reject) => {
6+
http.get(url, res => {
7+
const code = res.statusCode;
8+
if (code !== 200) {
9+
return reject(new Error(`HTTP status code ${code}`));
10+
}
1211

13-
res.on('error', reject);
12+
res.on('error', reject);
1413

15-
const chunks = [];
16-
res.on('data', chunk => {
17-
chunks.push(chunk);
18-
});
14+
const chunks = [];
15+
res.on('data', chunk => {
16+
chunks.push(chunk);
17+
});
1918

20-
res.on('end', () => {
21-
const json = Buffer.concat(chunks).toString();
22-
let object = null;
19+
res.on('end', () => {
20+
const json = Buffer.concat(chunks).toString();
21+
let object = null;
2322

24-
try {
25-
object = JSON.parse(json);
26-
} catch (error) {
27-
return reject(error);
28-
}
23+
try {
24+
object = JSON.parse(json);
25+
} catch (error) {
26+
return reject(error);
27+
}
2928

30-
resolve(object);
31-
});
29+
resolve(object);
3230
});
3331
});
34-
};
32+
});

JavaScript/promisify.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
'use strict';
22

3-
module.exports = function promisify(asyncFunction) {
4-
return (...args) => {
5-
return new Promise((resolve, reject) => {
6-
args.push((err, result) => {
7-
if (err) {
8-
reject(err);
9-
} else {
10-
resolve(result);
11-
}
12-
});
13-
asyncFunction(...args);
3+
module.exports = asyncFunction => (...args) => (
4+
new Promise((resolve, reject) => {
5+
args.push((err, result) => {
6+
if (err) reject(err);
7+
else resolve(result);
148
});
15-
};
16-
};
9+
asyncFunction(...args);
10+
})
11+
);

0 commit comments

Comments
 (0)