Skip to content

Commit

Permalink
commit lecture 2 live code
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbaile committed Jan 28, 2021
1 parent 693b1ce commit 523371c
Show file tree
Hide file tree
Showing 9 changed files with 2,361 additions and 0 deletions.
1 change: 1 addition & 0 deletions S2021/lecture2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2,120 changes: 2,120 additions & 0 deletions S2021/lecture2/PatientInfo.csv

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions S2021/lecture2/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// const sync = () => {
// console.log('first')
// console.log('second')
// }

// sync()

// const expensiveOperation = () => {
// setTimeout(() => {
// console.log('i am expensive')
// }, 5000)
// }

// const async = () => {
// console.log('first')
// expensiveOperation()
// console.log('second')
// }

// async() // -> how about now?


// setTimeout(() => {
// console.log('woah')
// }, 2)

// let x = 1
// while (x < 10000000000) {
// x = x + 1
// }
// console.log(x)

const expensiveOperation = () => {
setTimeout(() => {
console.log('i am expensive')
return 'data'
}, 5000)
}

const async = () => {
console.log('first')
const data = expensiveOperation()
console.log(`data: ${data}`)
}

async() // -> how about now?
27 changes: 27 additions & 0 deletions S2021/lecture2/axios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// READ APIs
const axios = require('axios')

// DOCS can be found on https://www.npmjs.com/package/axios

const data = axios.get('https://jsonplaceholder.typicode.com/todos/1')

// Approach 1: using promises
// const fetchDataPromise = () => {
// axios.get('https://jsonplaceholder.typicode.com/todos/1')
// .then(resp => {
// const { data } = resp
// console.log(data)
// })
// .catch(error => {
// console.log(error)
// })
// }

// Approach 2: still using promises + async/ await
const fetchDataAwait = async () => {
const { data } = await axios.get('https://jsonplaceholder.typicode.com/todos/1')
console.log(data)
}

// fetchDataPromise()
fetchDataAwait()
22 changes: 22 additions & 0 deletions S2021/lecture2/hw-readFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const csv = require('csv-parser')
const fs = require('fs')

const records = []

// const appendRecord = data => {
// records.push(data)
// }

const printRecord = () => {
// console.log(records)
}

// THE WAY WE DID IN HOMEWORK
fs.createReadStream('./PatientInfo.csv')
.pipe(csv())
.on('data', data => {
records.push(data)
})
.on('end', printRecord)

console.log(records)
39 changes: 39 additions & 0 deletions S2021/lecture2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions S2021/lecture2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "lecture2",
"version": "1.0.0",
"description": "",
"main": "async.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1",
"csv-parse": "^4.15.0",
"csv-parser": "^3.0.0"
}
}
68 changes: 68 additions & 0 deletions S2021/lecture2/readFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// READ FILES

const fs = require('fs')
const parse = require('csv-parse/lib/sync')
const util = require('util')

// const data = fs.readFile('./PatientInfo.csv', 'utf8', () => {})
// console.log('hello')
// console.log(data)
// console.log('hello2')

// Approach 1: READ FILE USING CALLBACK
const readCallback = () => {
const callback = function (err, data) {
const records = parse(data, {
columns: true,
skip_empty_lines: true
})

console.log(records)
}

fs.readFile('./PatientInfo.csv', 'utf8', callback)
}

// Approach 2: READ FILE USING PROMISES
const readPromise = () => {
// convert the function into the promise version
// reference: https://stackoverflow.com/questions/46867517/how-to-read-file-with-async-await-properly
const readFile = util.promisify(fs.readFile)

readFile('./PatientInfo.csv', 'utf8').then(data => {
const records = parse(data, {
columns: true,
skip_empty_lines: true
})

console.log(records)
})
}

// Approach 3: READ FILE USING PROMISES + async/ await
const readPromiseAsync = async () => {
// convert the function into the promise version
const readFile = util.promisify(fs.readFile)

const data = await readFile('./PatientInfo.csv', 'utf8')

const records = parse(data, {
columns: true,
skip_empty_lines: true
})

console.log(records)
}

const readSync = () => {
let patientCsv = fs.readFileSync()
let records = parse(patientCsv, {
columns: true,
skip_empty_lines: true
})
console.log(records)
}

// readCallback()
// readPromise()
readPromiseAsync()
21 changes: 21 additions & 0 deletions S2021/lecture2/readFileasync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs')
const parse = require('csv-parse/lib/sync')
const util = require('util')

const readPromiseAsync = async () => {
// convert the function into the promise version
const readFile = util.promisify(fs.readFile)

const data = await readFile('./PatientInfo.csv', 'utf8')

console.log(data)

const records = parse(data, {
columns: true,
skip_empty_lines: true
})

console.log(records)
}

readPromiseAsync()

0 comments on commit 523371c

Please sign in to comment.