diff --git a/AsynchronousBerantai.js b/AsynchronousBerantai.js new file mode 100644 index 0000000..43d523a --- /dev/null +++ b/AsynchronousBerantai.js @@ -0,0 +1,18 @@ +const { buyTollRoadCard, topUpBalance, useTollRoad } = require('./utils'); + +async function getTollAccess() +{ + try + { + const buy = await buyTollRoadCard(25); + const topUp = await topUpBalance(buy, 10); + const use = await useTollRoad(topUp); + } + + catch(error) + { + console.log(error.message); + } +} + +getTollAccess(); diff --git a/Promise-Based.js b/Promise-Based.js new file mode 100644 index 0000000..10e9f93 --- /dev/null +++ b/Promise-Based.js @@ -0,0 +1,29 @@ +const {promisify} = require('util'); + +function getProvinces(countryId, callback) +{ + setTimeout(() => + { + if (countryId === 'id') + { + callback(null, + [ + { id: 'id-jk', name: 'Jakarta' }, + { id: 'id-bt', name: 'Banten' }, + { id: 'id-jr', name: 'Jawa Barat' }, + ]); + + return; + } + + callback(new Error('Country not found'), null); + }, 1000); +} + +const getProvincesPromise = promisify(getProvinces); + +getProvincesPromise('id') +.then (Provinces => console.log(Provinces)) +.catch (err => console.log(err.message)); + +module.exports = { getProvinces: getProvincesPromise};