Home JS: Mengubah Fungsi Asynchronous Callback-Based menjadi Promise-Based
Post
Cancel

JS: Mengubah Fungsi Asynchronous Callback-Based menjadi Promise-Based

TODO

/**
 * @TODO
 * Ubahlah fungsi asynchronous callback-based getProvinces menjadi Promise-based.
 *
 * Catatan:
 * - Anda boleh menggunakan util.promisify untuk mengubah fungsi callback-based menjadi Promise-based.
 * - Jika nama fungsinya berubah, sesuaikan nilai yang diekspor tanpa mengubah nama properti di akhir berkas ini.
 */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const util = require('util');

function getProvinces(countryId) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (countryId === 'id') {
        resolve([
          { id: 'id-jk', name: 'Jakarta' },
          { id: 'id-bt', name: 'Banten' },
          { id: 'id-jr', name: 'Jawa Barat' },
        ]);
      } else {
        reject(new Error('Country not found'));
      }
    }, 1000);
  });
}

module.exports = { getProvinces };
This post is licensed under CC BY 4.0 by the author.