-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
peterbaile
committed
Jan 28, 2021
1 parent
693b1ce
commit 523371c
Showing
9 changed files
with
2,361 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |