Skip to content

Commit 2450f45

Browse files
committed
Update week-3 from in-class examples
1 parent 4e78d3e commit 2450f45

File tree

22 files changed

+192
-20
lines changed

22 files changed

+192
-20
lines changed

.vscode/launch.json

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${file}"
12+
},
713
{
814
"type": "node",
915
"request": "launch",
File renamed without changes.
File renamed without changes.
File renamed without changes.

week-3-1/index.js week-3-async-file-read/index.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
const fs = require('fs');
1+
const fs = require('fs')
22

33
/*
44
55
First version, synchronous
66
77
*/
88

9-
const file1 = fs.readFileSync(__dirname + '/files/1.txt', 'utf8');
10-
console.log(file1);
9+
const file1 = fs.readFileSync(__dirname + '/files/1.txt', 'utf8')
10+
console.log(file1)
1111

12-
const file2 = fs.readFileSync(__dirname + '/files/2.txt', 'utf8');
13-
console.log(file2);
12+
const file2 = fs.readFileSync(__dirname + '/files/2.txt', 'utf8')
13+
console.log(file2)
1414

15-
const file3 = fs.readFileSync(__dirname + '/files/3.txt', 'utf8');
16-
console.log(file3);
15+
const file3 = fs.readFileSync(__dirname + '/files/3.txt', 'utf8')
16+
console.log(file3)
1717

1818
/*
1919
@@ -27,9 +27,9 @@ fs.readFile(__dirname + '/files/1.txt', 'utf8', (err, contents1) => {
2727
console.log(contents2)
2828
fs.readFile(__dirname + '/files/3.txt', 'utf8', (err, contents3) => {
2929
console.log(contents3)
30-
});
31-
});
32-
});
30+
})
31+
})
32+
})
3333

3434

3535
/*
@@ -41,19 +41,20 @@ Third version, promises
4141
let readFile = (filename) => {
4242
return new Promise((resolve, reject) => {
4343
fs.readFile(filename, 'utf8', (err, contents) => {
44-
if (err) return reject(err);
44+
if (err) return reject(err)
4545

46-
resolve(contents);
47-
});
48-
});
46+
resolve(contents)
47+
})
48+
})
4949
}
5050

5151
readFile(__dirname + '/files/1.txt')
5252
.then(console.log)
5353
.then(() => readFile(__dirname + '/files/2.txt'))
5454
.then(console.log)
5555
.then(() => readFile(__dirname + '/files/3.txt'))
56-
.then(console.log);
56+
.then(console.log)
57+
.catch(err => console.log(err))
5758

5859
/*
5960
@@ -72,4 +73,10 @@ const main = async () => {
7273
console.log(contents3)
7374
}
7475

75-
main()
76+
(async () => {
77+
try {
78+
await main()
79+
} catch (e) {
80+
console.log(e)
81+
}
82+
})()

week-3-1/package-lock.json week-3-async-file-read/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

week-3-1/package.json week-3-async-file-read/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "week-3-1",
2+
"name": "week-3-async-file-read",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",

week-3-map-objects/data.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Women Tech Makers Berlin","location":"Eurostaff","attendees":[{"name":"Armagan","age":34},{"name":"Mert","age":33}]}

week-3-map-objects/database.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fs = require('fs')
2+
module.exports = {
3+
save(data) {
4+
// console.log(data)
5+
fs.writeFileSync('data.json', JSON.stringify(data))
6+
},
7+
load() {
8+
return JSON.parse(fs.readFileSync('data.json'))
9+
}
10+
}

week-3-map-objects/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Person = require('./person')
2+
const Meetup = require('./meetup')
3+
const Database = require('./database')
4+
5+
console.log('Hello World!')
6+
7+
const mert = new Person('Mert', 33)
8+
const armagan = new Person('Armagan', 34)
9+
// console.log(mert, armagan)
10+
11+
const wtmb = new Meetup('Women Tech Makers Berlin', 'Eurostaff')
12+
armagan.attend(wtmb)
13+
mert.attend(wtmb)
14+
wtmb.report()
15+
16+
Database.save(wtmb)
17+
const loadedFile = Database.load()
18+
console.log(loadedFile.attendees[0].name)

week-3-map-objects/meetup.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Chalk = require('chalk')
2+
const Person = require('./person')
3+
4+
module.exports = class Meetup {
5+
constructor(name, location) {
6+
this.name = name
7+
this.location = location
8+
this.attendees = []
9+
}
10+
11+
report() {
12+
console.log(Chalk.blue.bgRed.bold(this.name), 'meetup is held at', Chalk.green(this.location), 'and number of attendees are', this.attendees.length)
13+
}
14+
15+
static create({ name, location, attendees }) {
16+
const meetup = new Meetup(name, location)
17+
18+
meetup.attendees = attendees.map(Person.create)
19+
20+
return meetup
21+
}
22+
}

week-3-map-objects/package-lock.json

+57
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

week-3-map-objects/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "week-3-map-objects",
3+
"version": "1.0.0",
4+
"description": "WTM Berlin JS Crash Course 2018 Lecture 2 - Node.js",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Mert Cetin <[email protected]>",
10+
"license": "MIT",
11+
"dependencies": {
12+
"chalk": "^2.4.1"
13+
}
14+
}

week-3-map-objects/person.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = class Person {
2+
constructor(name, age) {
3+
this.name = name
4+
this.age = age
5+
}
6+
7+
attend(meetup) {
8+
meetup.attendees.push(this)
9+
}
10+
11+
static create({ name, age }) {
12+
return new Person(name, age)
13+
}
14+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

week-3-2/package-lock.json week-3-models-and-services/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

week-3-2/package.json week-3-models-and-services/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "week-3-2",
2+
"name": "week-3-models-and-services-2",
33
"version": "1.0.0",
44
"description": "WTM Berlin JS Crash Course 2018 Lecture 3 - async programming",
55
"main": "index.js",
File renamed without changes.

week-3-timeout-example/index.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
console.log('1')
2+
3+
setTimeout(() => {
4+
console.log('2')
5+
console.log('5')
6+
}, 10000)
7+
8+
console.log('3')
9+
console.log('4')
10+
console.log('6')
11+
12+
setTimeout(() => {
13+
console.log('7')
14+
}, 7000)
15+
16+
setTimeout(() => {
17+
console.log('0')
18+
}, 3000)
19+
20+
21+
process.on('SIGINT', () => {
22+
console.log('i need to exit gracefully!')
23+
})

0 commit comments

Comments
 (0)