Last active
January 29, 2018 14:21
-
-
Save bepitulaz/4340bb8176769d1eddc9b2862ebdae70 to your computer and use it in GitHub Desktop.
Mengenal Promise pada 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
| /** | |
| Buat object dari Promise-nya. | |
| */ | |
| var janjiBunga = new Promise(function(resolve, reject) { | |
| var http = new XMLHttpRequest(); | |
| var url = '/bunga'; | |
| var params = 'jumlah=1000000'; | |
| http.open('POST', url, true); | |
| http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); | |
| http.onreadystatechange = function() { | |
| if(http.readyState == 4 && http.status == 200) { | |
| resolve(1000000); // 2. Uangmu sukses dikembalikan oleh Bunga (Ini value-nya hardcoded.) | |
| } | |
| } | |
| http.onerror = function() { | |
| reject('Saya belum bisa bayar. Proyek saya gagal.'); // 3. Ketika gagal Bunga akan memberikan alasan | |
| } | |
| http.send(params); // 1. Kamu mengirimkan uang pada Bunga | |
| }); | |
| console.log(janjiBunga); // hasilnya Promise { <Pending> }, karena operasi httprequest di atas belum selesai. | |
| /** | |
| Sekarang saatnya kamu menagih Bunga. | |
| Methodnya bernama .then() untuk memproses hasil yang sukses dan .catch() untuk menerima error. | |
| */ | |
| janjiBunga.then(function(uang) { | |
| console.log(uang); // isinya 1 juta karena operasinya fulfilled | |
| }).catch(function(error) { | |
| console.log(error); // isinya 'Saya belum bisa bayar. Proyek saya gagal.' | |
| }); | |
| /** | |
| Fungsi di bawah ini untuk mensimulasikan permintaan Lebah. | |
| */ | |
| function lebahInginBerhutang(janji) { | |
| janji.then(function(uang) { | |
| console.log(uang); // Isinya 1 juta | |
| }).catch(function(error) { | |
| console.log(error); // Isinya 'Saya belum bisa bayar. Proyek saya gagal.' | |
| }); | |
| } | |
| // Kamu menyuruh Lebah untuk menagih uangmu dengan melempar Promise sebagai argumen | |
| lebahInginBerhutang(janjiBunga); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wih mantap