Created
January 19, 2021 09:10
-
-
Save GulajavaMinistudio/1cec99e2f282f30129945a7bdfdb71d1 to your computer and use it in GitHub Desktop.
Contoh chain promise AllSettled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// const result = []; | |
// result.forEach((item, index) => { | |
// db.collection("pis_list_screen") | |
// .findOne({ | |
// destination: item.lineDestination, | |
// stopkey: item.stopKey, | |
// }) | |
// .then( | |
// function (items) { | |
// return (scodeku = items.scode); | |
// }, | |
// function (err) { | |
// console.error("The promise was rejected", err, err.stack); | |
// } | |
// ); | |
// }); | |
function queryDataAll() { | |
const arrayPromiseFindOne = []; | |
result.forEach((item, index) => { | |
const findOnePromise = db.collection("pis_list_screen").findOne({ | |
destination: item.lineDestination, | |
stopkey: item.stopKey, | |
}); | |
arrayPromiseFindOne.push(findOnePromise); | |
}); | |
olahDataQuery(arrayPromiseFindOne); | |
} | |
// Menggunakan Promise AllSettled | |
function olahDataQuery(arrPromise) { | |
Promise.allSettled(arrPromise) | |
.then((results) => { | |
olahResultPromise(results); | |
}) | |
.catch((error) => { | |
console.log(error); | |
}); | |
} | |
function olahResultPromise(arrResults) { | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled | |
const intPanjangResult = arrResults.length; | |
const arrayResult = []; | |
for (let i = 0; i < intPanjangResult; i += 1) { | |
const result = arrResults[i]; | |
if (result.status === 'fulfilled') { | |
arrayResult.push(arrayResult.value); | |
} | |
} | |
hasilResultPromise(arrayResult); | |
} | |
function hasilResultPromise(arrResult) { | |
console.log(arrResult); | |
} | |
// Jalankan dengan memanggil fungsi paling awal | |
queryDataAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment