Skip to content

Instantly share code, notes, and snippets.

@bepitulaz
Last active January 16, 2017 06:10
Show Gist options
  • Select an option

  • Save bepitulaz/0f745f1a1436ee7a389033b03e1c908c to your computer and use it in GitHub Desktop.

Select an option

Save bepitulaz/0f745f1a1436ee7a389033b03e1c908c to your computer and use it in GitHub Desktop.
Promise bagian 2
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);
});
// masukkan kedua object Promise di atas ke dalam array
var batchProcessed = [promiseOne, promiseTwo];
Promise.race(batchProcessed).then(function(result) {
// result akan segera ada isinya begitu salah satu Promise fulfilled
console.log(result); // Yang pertama kali resolve yang akan tampil: "promise 1 sukses"
}).catch(function(error) {
// error akan segera terisi saat ada yang rejected
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment