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
/** | |
MASALAH: | |
Anggap kita punya 3 asynchronous function yang harus dijalankan berurutan. | |
Jadilah callback hell seperti di bawah ini. | |
Apalagi jika kita harus menjalankan operasi tertentu di antara | |
callback-callback tersebut yang membuat kode menjadi panjang, dan tentunya | |
makin sulit dibaca. | |
*/ | |
jQuery.get('http://example.com/data.json', function(response) { | |
// lakukan sesuatu dengan `response` dari operasi 1 |
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
/** | |
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'); |
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
var samplePromise = new Promise(function(resolve, reject) { | |
// setTimeout untuk mensimulasikan async function | |
setTimeout(function() { | |
resolve(5); | |
}, 5); | |
}); | |
samplePromise.then(function(data) { | |
console.log(data); // isinya 5 | |
return data * 2; |
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
var samplePromise = new Promise(function(resolve, reject) { | |
// setTimeout untuk mensimulasikan async function | |
setTimeout(function() { | |
// Dapat diisi apapun. Yang paling umum menggunakan object Error | |
reject(Error('Sengaja dibuat salah.')); | |
}, 5); | |
}); | |
samplePromise.then(function(data) { | |
console.log(data); // tidak terpanggil |
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
var promiseOne = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve('promise 1 sukses'); | |
}, 2000); | |
}); | |
var promiseTwo = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve('promise 2 sukses'); | |
}, 3000); |
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
var promiseOne = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve('promise 1 sukses'); | |
}, 2000); | |
}); | |
var promiseTwo = new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve('promise 2 sukses'); | |
}, 3000); |
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
/** | |
* Fungsi ini digunakan untuk menghitung harga barang setelah diskon. | |
* @param {Number} discountRate - nilai diskon yang diberikan | |
* @param {Number} originalPrice - harga barang sebelum diskon dalam Rupiah | |
* @returns {Number} | |
*/ | |
function calculateDiscount(originalPrice, discountRate) { | |
let discount = originalPrice * discountRate / 100; | |
return originalPrice - discount; | |
} |
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
#define LEDPIN 2 | |
void setup() | |
{ | |
pinMode(LEDPIN, OUTPUT); | |
} | |
void loop() | |
{ | |
digitalWrite(LEDPIN, HIGH); // led menyala |
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
(function() { | |
let audioCtx = new (window.AudioContext || window.webkitAudioContext)(); | |
// Create the oscillator | |
let osc1 = audioCtx.createOscillator(); | |
let osc2 = audioCtx.createOscillator(); | |
// Create the gain node | |
let gainOsc1 = audioCtx.createGain(); | |
let gainOsc2 = audioCtx.createGain(); |
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
(function() { | |
let audioCtx = new (window.AudioContext || window.webkitAudioContext)(); | |
// Create the oscillator | |
let osc1 = audioCtx.createOscillator(); | |
// Create the gain node | |
let gainOsc1 = audioCtx.createGain(); | |
// Create the panner |