Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Dony Salman Adi Pradana
- //No 1
- const myCountPromise = (value) => {
- return new Promise((resolve, reject) => {
- if (value != undefined) {
- setTimeout(() => {
- resolve(value*2);
- },2000);
- } else {
- reject("Maaf tidak ada nilai dalam parameter");
- }
- });
- };
- myCountPromise(2)
- .then((result) => {
- console.log(result);
- })
- .catch((error) => {
- console.log(error);
- });
- //No 2
- function filterBooksPromise(colorful, amountOfPage){
- return new Promise(function(resolve, reject){
- var books=[
- {name: "shinchan", totalPage: 50, isColorful: true},
- {name: "Kalkulus", totalPage: 250, isColorful: false},
- {name: "doraemon", totalPage: 40, isColorful: true},
- {name: "algoritma", totalPage: 250, isColorful: false},
- ]
- if (amountOfPage >= 40) {
- resolve(books.filter(x=> x.totalPage >= amountOfPage && x.isColorful == colorful));
- } else {
- var reason= "Maaf buku di bawah 40 halaman tidak tersedia"
- reject(reason);
- }
- });
- }
- // jalankan function promisenya disini ( wajib menggunakan async/await )
- const execute = async (colorful, amountOfPage) => {
- try {
- const result = await filterBooksPromise(colorful, amountOfPage);
- console.log(result);
- } catch (error) {
- console.log(error);
- }
- }
- execute(true, 40);
- execute(false, 250);
- execute(true, 30);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement