Created
January 4, 2017 13:29
-
-
Save bepitulaz/adfec8e722b8d14c57d81d3c3e161fba to your computer and use it in GitHub Desktop.
Callback VS Promise
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 yang tidak membutuhkan callback | |
function sum(a, b) { | |
return a + b; | |
} | |
// isi dari variabel result adalah 6 | |
var result = sum(1, 5); | |
/** | |
Di bawah ini adalah contoh function yang membutuhkan callback. | |
Saya ambil contoh jQuery.get(). | |
Ini adalah function dari pustaka jQuery untuk melakukan pemanggilan data menggunakan ajax. | |
*/ | |
var noCallback = jQuery.get('http://example.com/data.json'); | |
console.log(noCallback); | |
// Isi dari variabel noCallback di atas adalah 'undefined', karena pemanggilan fungsi jQuery.get() mungkin belum selesai | |
// saat baris console.log() dieksekusi. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment