Facebook
From Marsa Sefta Lesmana, 1 Month ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 140
  1. function filterBookPromise(colorful, amountOfPage) {
  2.   return new Promise(function (resolve, reject) {
  3.     var books = [
  4.       { name: "shinchan", totalPage: 50, isColorful: true },
  5.       { name: "kalkulus", totalPage: 250, isColorful: false },
  6.       { name: "doraemon", totalPage: 40, isColorful: true },
  7.       { name: "algoritma", totalPage: 250, isColorful: false },
  8.     ];
  9.     if (amountOfPage >= 40) {
  10.       resolve(
  11.         books.filter(
  12.           (x) => x.totalPage >= amountOfPage && x.isColorful == colorful
  13.         )
  14.       );
  15.     } else {
  16.       var reason = "Maaf buku dibawah 40 halaman tidak tersedia";
  17.       reject(reason);
  18.     }
  19.   });
  20. }
  21.  
  22. async function example(colorful, totalPage) {
  23.   try {
  24.     const result = await filterBookPromise(colorful, totalPage);
  25.     console.log(result);
  26.   } catch (error) {
  27.     console.error(error);
  28.   }
  29. }
  30. example(true, 40);
  31. example(false, 250);
  32. example(true, 30);
  33.