Advertisement
pradana17

LiveSessionSanbercodeReactJs

Mar 17th, 2025
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Dony Salman Adi Pradana
  2.  
  3. //No 1
  4. const myCountPromise = (value) => {
  5.     return new Promise((resolve, reject) => {
  6.         if (value != undefined) {
  7.             setTimeout(() => {
  8.                 resolve(value*2);
  9.             },2000);
  10.         } else {
  11.             reject("Maaf tidak ada nilai dalam parameter");
  12.         }
  13.     });
  14. };
  15.  
  16. myCountPromise(2)
  17.     .then((result) => {
  18.         console.log(result);
  19.     })
  20.     .catch((error) => {
  21.         console.log(error);
  22.     });
  23.  
  24.  
  25. //No 2
  26. function filterBooksPromise(colorful, amountOfPage){
  27.     return new Promise(function(resolve, reject){
  28.     var books=[
  29.     {name: "shinchan", totalPage: 50, isColorful: true},
  30.     {name: "Kalkulus", totalPage: 250, isColorful: false},
  31.     {name: "doraemon", totalPage: 40, isColorful: true},
  32.     {name: "algoritma", totalPage: 250, isColorful: false},
  33.     ]
  34.     if (amountOfPage >= 40) {
  35.     resolve(books.filter(x=> x.totalPage >= amountOfPage && x.isColorful == colorful));
  36.     } else {
  37.     var reason= "Maaf buku di bawah 40 halaman tidak tersedia"
  38.     reject(reason);
  39.     }
  40.     });
  41.     }
  42.      
  43.    
  44.     // jalankan function promisenya disini ( wajib menggunakan async/await )
  45.  
  46.     const execute = async (colorful, amountOfPage) => {
  47.         try {
  48.             const result = await filterBooksPromise(colorful, amountOfPage);
  49.             console.log(result);
  50.         } catch (error) {
  51.             console.log(error);
  52.         }
  53.     }
  54.  
  55.     execute(true, 40);
  56.     execute(false, 250);
  57.     execute(true, 30);
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement