TekenaJa

const axios = require('axios')

let tekenUrl = 'https://apix.sandbox-111094.com/v2/data'
let tekenApiKey = ''

const tekenProvince = async () => {
	const resProvince = await axios.get(`${tekenUrl}/province`, {
		headers: { apikey: tekenApiKey }
	})

	return Object.keys(resProvince.data.data).map((val, index) => {
		return { id: val, name: Object.values(resProvince.data.data)[index] }
	})
}

const tekenDistrict = async () => {
	const data = await tekenProvince()
	let listData = []

	const districtData = await data.map(async (val) => {
		const resDistrict = await axios.get(`${tekenUrl}/district?province=${val.id}`, {
			headers: { apikey: tekenApiKey }
		})

		const newDistrictData = []
		Object.keys(resDistrict.data.data).forEach((district, index) => {
			newDistrictData.push({ id: district, name: Object.values(resDistrict.data.data)[index] })
		})
		return newDistrictData
	})

	for (let i in districtData) {
		listData.push({ province: data[i], district: await districtData.flat(Infinity)[i] })
	}

	return listData
}

const tekenSubDistrict = async () => {
	let dukcapil = []

	const district = await tekenDistrict()
	const data = await district.map(async ({ province, district }) => {
		let listData = []

		let subdistrictData = await district.map(async ({ id }) => {
			const resSubdistrict = await axios.get(`${tekenUrl}/subdistrict?province=${province.id}&district=${id}`, {
				headers: { apikey: tekenApiKey }
			})
			return resSubdistrict.data.data
		})

		for (let i in subdistrictData) {
			listData.push(await subdistrictData[i])
		}

		return listData
	})

	for (let i in data) {
		dukcapil.push(await data[i])
	}

	return dukcapil
}

;(async () => {
	const data = await tekenSubDistrict()
	console.log(data.length)
})()
Restu Wahyu Saputra