forked from and-digital/and-workshop-corejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_callback.js
73 lines (54 loc) · 1.39 KB
/
08_callback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//## CALLBACKS
//## SYNCHRONOUS CALLBACKS
let numbers = [2, 4, 8, 10];
let halves = numbers.map(function(x) {
x / 2;
});
// halves is now [1, 2, 4, 5]
// numbers is still [2, 4, 8, 10]
numbers = [1, 4, 9];
roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
const arr = ['a', 'b', 'c'];
arr.forEach(function(element) {
console.log(element);
});
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// rl.question('What do you think of Node.js? ', function(answer) {
// console.log(`Thank you for your valuable feedback: ${answer}`);
// rl.close();
// });
// rl.question('All good? ', logAnswer);
// function logAnswer(answer) {
// console.log(`Thank you for your valuable feedback: ${answer}`);
// rl.close();
// }
//## ASYNCHRONOUS CALLBACK
const http = require('http');
const httpOptions = {
hostname: 'jsonplaceholder.typicode.com',
port: 80,
path: '/comments'
};
http.get(httpOptions, function(res) {
let data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
//console.log(JSON.parse(data));
});
});
const fs = require('fs');
fs.readdir('..', function(err, files) {
console.log(files);
});
fs.readdir('..', listFiles);
function listFiles(err, files) {
console.log('\nfiles from listFiles function', files);
}