Skip to content

Commit 42d350c

Browse files
committed
update
1 parent f804e16 commit 42d350c

8 files changed

+218
-0
lines changed

async_JS/01_setup.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// JS is single threaded, synchronous language
2+
console.log('one');
3+
console.log('two');
4+
console.log('three');
5+
stillProcess();
6+
console.log('fours');
7+
8+
function stillProcess() {
9+
for (let i = 0; i < 1000; i++) {
10+
console.log(`still processs....`);
11+
}
12+
console.log('done');
13+
}

async_JS/02_recipes-example.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// make soup
2+
// boil wate 10 min
3+
// chop onion
4+
// add carrots boil for 5 min
5+
// chop carrots
6+
// add onion boil for 5 min
7+
8+
boilWaters(1000);
9+
console.log('chop onion');
10+
boilWaters(5000);
11+
console.log('chop carrots');
12+
boilWaters(5000);
13+
14+
function boilWaters(time) {
15+
console.log(`boiling....`);
16+
for (let i = 0; i < time; i++) {
17+
console.log('still not done');
18+
}
19+
console.log('done...');
20+
}

async_JS/03_asynchronous.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// make soup
2+
// boil wate 10 min
3+
// chop onion
4+
// add carrots boil for 5 min
5+
// chop carrots
6+
// add onion boil for 5 min
7+
8+
// disini dia mencetak
9+
// boil wate.....
10+
// chop onion.....
11+
// kemudian still busy menghitung counter sampai 10000
12+
// jika sudah baru menuju ke setTimeout done
13+
// karena javascript membaca perbaris line by line
14+
// mau bolWaters(0), atau 1000 dia tetap membaca baris perbaris
15+
// jadi ketika loop selesai otomatis setTimeout berjalan terakhir
16+
boilWaters(0);
17+
console.log('chop onion');
18+
for (let i = 0; i < 1000; i++) {
19+
console.log('still busy....');
20+
}
21+
22+
function boilWaters(time) {
23+
console.log(`boil wate.....`);
24+
setTimeout(() => {
25+
console.log('done');
26+
}, time);
27+
}

async_JS/04_callback_hell..js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// callback hell
2+
3+
boilWater();
4+
console.log(`chop carrot`); // yg kedua dijalankan
5+
6+
function boilWater() {
7+
console.log(`boiling....`); //prtama dijalankan krna invoke
8+
// 1
9+
setTimeout(() => {
10+
console.log(`done...`); // dia running ke 3
11+
console.log(`add carrot`); // dia running ke 4
12+
//2
13+
setTimeout(() => {
14+
console.log(`carrot done`); //dia running ke 6
15+
console.log(`add onion`); //dia running ke 7
16+
//3
17+
setTimeout(() => {
18+
console.log(`onion done`); /// dan terakhir adalah done
19+
}, 10000); // waiting onion... 10 detik sebelum masuk sini
20+
console.log(`waiting onion.....`); //dia running ke 8 dan mnunggu 10 detik setTimeout ke 3
21+
}, 10000); // chop onions.. waiting 10detik sebelum masuk kesini
22+
console.log(`chop onions...waiting`); //dia running ke 5 dan mnunggu 10 detik setTimeout ke 2
23+
}, 100);
24+
}
25+
26+
// Notes:
27+
/*
28+
boiling....
29+
chop carrot
30+
done...
31+
add carrot
32+
chop onions...waiting //setTimeout 1 mnunggu 5detik
33+
carrot done
34+
add onion
35+
waiting onion..... //setTimeout 2 mnunggu 5detik
36+
onion done
37+
*/

async_JS/05_callback_hellDOM.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// callback dom
2+
3+
const heading1 = document.querySelector('.one');
4+
const heading2 = document.querySelector('.two');
5+
const heading3 = document.querySelector('.three');
6+
7+
const btn = document.querySelector('.btn');
8+
9+
btn.addEventListener('click', function () {
10+
setTimeout(() => {
11+
heading1.style.color = 'red';
12+
setTimeout(() => {
13+
heading2.style.color = 'green';
14+
setTimeout(() => {
15+
heading3.style.color = 'blue';
16+
}, 3000);
17+
}, 2000);
18+
}, 1000);
19+
});

async_JS/06_promises.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// callbacks, promises, async/await
2+
// promises - pending, resolved, rejected
3+
// then catch - pass another callback
4+
const heading1 = document.querySelector('.one');
5+
const heading2 = document.querySelector('.two');
6+
const heading3 = document.querySelector('.three');
7+
8+
const btn = document.querySelector('.btn');
9+
10+
btn.addEventListener('click', () => {});
11+
12+
// promises pending
13+
// disini dia akan menghasilkan pending
14+
// const promise = new Promise((resolve, reject) => {});
15+
// console.log(promise);
16+
17+
// promises
18+
// resolve, reject is callback function
19+
const promise = new Promise((resolve, reject) => {
20+
let value = false;
21+
if (value) {
22+
resolve(`This will true value`);
23+
} else {
24+
reject(`Error i'm sorry... cause not true`);
25+
}
26+
});
27+
28+
// Tidak bisa mengakses sperti ini
29+
// console.log(promise.value); //undefined
30+
// solusi jika ingin tau sukses , or error for value
31+
// gunakan then (sucess), dan catch tuk error value
32+
// dan tuk callbackfunction massing2 tsb whatever
33+
// karena dia akan merujuk pada value yg true/false
34+
promise
35+
.then((justTrue) => {
36+
console.log(justTrue);
37+
})
38+
.catch((errrrr) => {
39+
console.log(errrrr); //Error i'm sorry... cause not true
40+
});

async_JS/index copy.html

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
<title>Document</title>
7+
</head>
8+
<style>
9+
h1 {
10+
text-align: center;
11+
font-weight: bold;
12+
}
13+
.btn {
14+
margin: 0 auto;
15+
display: grid;
16+
justify-content: center;
17+
padding: 10px;
18+
letter-spacing: 1.2px;
19+
font-size: 1rem;
20+
cursor: pointer;
21+
}
22+
</style>
23+
<body>
24+
<h1>Asynchronous JavaScript</h1>
25+
<h1 class="one">Heading ONE</h1>
26+
<h1 class="two">Heading TWO</h1>
27+
<h1 class="three">Heading THREE</h1>
28+
<button class="btn">Change Colors</button>
29+
</body>
30+
<script src="./05_callback_hellDOM.js"></script>
31+
</html>

async_JS/index.html

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
<title>Document</title>
7+
</head>
8+
<style>
9+
h1 {
10+
text-align: center;
11+
font-weight: bold;
12+
}
13+
.btn {
14+
margin: 0 auto;
15+
display: grid;
16+
justify-content: center;
17+
padding: 10px;
18+
letter-spacing: 1.2px;
19+
font-size: 1rem;
20+
cursor: pointer;
21+
}
22+
</style>
23+
<body>
24+
<h1>Asynchronous JavaScript</h1>
25+
<h1 class="one">Heading ONE</h1>
26+
<h1 class="two">Heading TWO</h1>
27+
<h1 class="three">Heading THREE</h1>
28+
<button class="btn">Change Colors</button>
29+
</body>
30+
<script src="./06_promises.js"></script>
31+
</html>

0 commit comments

Comments
 (0)