Skip to content

Commit 3536238

Browse files
committed
Lecture 08
1 parent 3218ca5 commit 3536238

File tree

8 files changed

+157
-0
lines changed

8 files changed

+157
-0
lines changed

Lecture08/async/counter.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const start = Date.now()
2+
3+
setInterval(() => {
4+
const shown = Date.now() - start
5+
}, 1000)

Lecture08/async/file.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function download (url, callback) {
2+
let file = "Some data here"
3+
setTimeout(() => {
4+
callback(null, file)
5+
}, 3000)
6+
}
7+
8+
function resize (file, callback) {
9+
let resizedFile = "Compressed Stuff Here"
10+
setTimeout(() => {
11+
callback(null, resizedFile)
12+
}, 5000)
13+
}
14+
15+
function upload (file, callback) {
16+
let url = "cb.lk/file.jpg"
17+
setTimeout(() => {
18+
callback(null, url)
19+
}, 10000)
20+
}
21+
22+
23+
download('cb.lk/secret' , (err, file) => {
24+
if (err) {
25+
// handleError
26+
}
27+
resize(file, resizedFile => {
28+
upload(resizedFile, url => {
29+
console.log(url)
30+
})
31+
})
32+
})

Lecture08/async/hof.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// const generatePromise = (val, n) =>
2+
// new Promise((resolve) => setTimeout(() => resolve(val), n))
3+
4+
// const generatePromise = function (val, n) {
5+
// const fn = function (resolve) {
6+
// setTimeout(function () {
7+
// resolve(val)
8+
// }, n)
9+
// }
10+
// return new Promise(fn)
11+
// }
12+
13+
14+
// generatePromise(1, 1000).then(console.log)
15+
// generatePromise(2, 2000).then(console.log)
16+
// generatePromise(3)(3000).then(console.log)
17+
// generatePromise(4, 4000).then(console.log)
18+
19+
const generatePromiseHOF = val => n =>
20+
new Promise((resolve) => setTimeout(() => resolve(val), n))
21+
22+
const resolve3After = generatePromiseHOF(3)
23+

Lecture08/async/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// console.log(1)
2+
// console.log(2)
3+
4+
5+
6+
setTimeout(() => {
7+
console.log("after 1 sec")
8+
}, 1000)
9+
10+
for (let i=0;i < 1e5; i++) {
11+
console.log()
12+
}
13+
14+
console.log("Done")

Lecture08/async/multiple.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// setTimeout(() => {
2+
// console.log(1)
3+
// }, 0)
4+
5+
// setTimeout(() => {
6+
// console.log(2)
7+
// }, 0)
8+
9+
const id = setInterval(() => {
10+
console.log("every 1 sec")
11+
}, 1000)
12+
13+
setTimeout(() => {
14+
clearInterval(id)
15+
}, 5100)
16+
17+
console.log(3)

Lecture08/async/promise.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function download (url) {
2+
const promise = new Promise((resolve, reject) => {
3+
setTimeout(() => {
4+
reject("New Error")
5+
}, 1000)
6+
})
7+
return promise
8+
}
9+
10+
function resize (file) {
11+
const promise = new Promise((resolve) => {
12+
setTimeout(() => {
13+
resolve("Resized File")
14+
}, 2000)
15+
})
16+
return promise
17+
}
18+
19+
function upload (file) {
20+
const promise = new Promise((resolve) => {
21+
setTimeout(() => {
22+
resolve("cb.lk/secret.jpg")
23+
}, 5000)
24+
})
25+
return promise
26+
}
27+
28+
29+
const downloadedFile = download('cb.lk/file.jpg')
30+
const promise = downloadedFile.then(file => {
31+
return resize(file)
32+
}).catch(err => {
33+
34+
})
35+
.then(resizedFile => {
36+
return upload(resizedFile)
37+
}).then(url => {
38+
console.log(url)
39+
}).catch(err => {
40+
console.log(err)
41+
}).then(() => {
42+
console.log("Always")
43+
throw new Error("Some err")
44+
}).catch(err => {
45+
console.log("error Again")
46+
})

Lecture08/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<button id="btn">Click Me</button>
11+
<script src="index.js"></script>
12+
</body>
13+
</html>

Lecture08/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const button = document.getElementById('btn')
2+
3+
button.onclick = function () {
4+
console.log("clicked")
5+
}
6+
7+
console.log("JS Finished")

0 commit comments

Comments
 (0)