Created
March 24, 2023 10:41
-
-
Save solanoize/c9ede74cce80f682de4167969ed61207 to your computer and use it in GitHub Desktop.
Asynchronous Programming di JavaScript
This file contains hidden or 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
| // Aku akan menyelesaikannya nanti! | |
| // Fungsi yang berjalan paralel dengan fungsi lain disebut asinkron | |
| // set time out | |
| setTimeout(() => { | |
| document.getElementById("demo").innerHTML = "Urutan 1"; | |
| }, 3000); | |
| setTimeout(() => { | |
| document.getElementById("demo").innerHTML = "Urutan 2"; | |
| }, 5000); | |
| document.getElementById("demo").innerHTML = "Urutan 3" |
This file contains hidden or 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
| // set interval | |
| let interval01 = 0; | |
| let interval02 = 0; | |
| let task1 = setInterval(() => { | |
| document.getElementById("demo1").innerHTML = `Task 1: ${interval01++}`; | |
| }, 3000); | |
| let task2 = setInterval(() => { | |
| document.getElementById("demo2").innerHTML = `Task 2: ${interval02++}`; | |
| }, 5000); | |
| document.getElementById("demo1").addEventListener("click", () => { | |
| clearInterval(task1); | |
| }) | |
| document.getElementById("demo2").addEventListener("click", () => { | |
| clearInterval(task2); | |
| }) |
This file contains hidden or 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
| setInterval(myFunction, 1000); | |
| function myFunction() { | |
| let d = new Date(); | |
| document.getElementById("demo").innerHTML= | |
| d.getHours() + ":" + | |
| d.getMinutes() + ":" + | |
| d.getSeconds(); | |
| } |
This file contains hidden or 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
| // i will promise to send result | |
| let sales = { | |
| harga: 20000, | |
| dibayar: 20000, | |
| selesai: false, | |
| status: "none" | |
| } | |
| let promise = new Promise(function(resolve, reject) { | |
| setTimeout(() => { | |
| if (sales.harga <= sales.dibayar) { | |
| sales.selesai = true; | |
| sales.status = "success"; | |
| resolve(sales); | |
| } else { | |
| sales.status = "fail"; | |
| reject(sales); | |
| } | |
| }, 5000) | |
| }); | |
| // not work, this is sequence control | |
| if (sales.status === "success") { | |
| alert("success"); | |
| } else if (sales.status === "fail") { | |
| alert("fail"); | |
| } | |
| // work, this is style of promise (async) | |
| promise.then((value) => { | |
| alert(value.status); | |
| }).catch((error) => { | |
| alert(error.status); | |
| }); | |
| alert("running program"); |
This file contains hidden or 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
| // using await for program promise (async) like control sequence in function | |
| let promise = new Promise((resolve, reject) => { | |
| let sales = { | |
| harga: 20000, | |
| dibayar: 20000, | |
| selesai: false, | |
| status: "none" | |
| }; | |
| setTimeout(() => { | |
| if (sales.harga <= sales.dibayar) { | |
| sales.selesai = true; | |
| sales.status = "success"; | |
| resolve(sales); | |
| } else { | |
| sales.status = "fail"; | |
| reject(sales); | |
| } | |
| }, 5000) | |
| }); | |
| async function main() { | |
| let result = await promise; | |
| alert(result.status); | |
| } | |
| main(); | |
| alert("running now") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment