-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy path2-callback-use.js
More file actions
39 lines (28 loc) · 848 Bytes
/
2-callback-use.js
File metadata and controls
39 lines (28 loc) · 848 Bytes
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
'use strict';
// In sync operations
{
const boxes = [{ count: 3 }, { count: 0 }, { count: 7 }];
const goods = boxes.filter(({ count }) => count > 0);
console.log({ goods });
}
// Named callbacks
{
const boxes = [{ count: 3 }, { count: 0 }, { count: 7 }];
const notEmpty = ({ count }) => count > 0;
const goods = boxes.filter(notEmpty);
console.log({ goods });
}
// In async operations
const boxes = [{ count: 3 }, { count: 0 }, { count: 7 }];
const timer = setInterval(() => {
const next = boxes.shift();
console.log(`Next box contains: ${next.count}`);
if (boxes.length === 0) clearInterval(timer);
}, 10);
// In async I/O
const fs = require('node:fs');
fs.readFile('./1-callback.js', 'utf8', (err, data) => {
if (err) console.error(err);
console.log({ lines: data.split('\n').length });
});
console.log('end');