Last active
January 16, 2017 06:10
-
-
Save bepitulaz/0f745f1a1436ee7a389033b03e1c908c to your computer and use it in GitHub Desktop.
Promise bagian 2
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
| 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