All we are trying to do is wait for the three getMy___ functions to finish before we start working with the final uniqueOffers code (these three functions each loop through the dedupOffers function along the way). We need the final value of the $w(‘#testDedup’) element before we can set up a new array and loop through that to get our final desired data. But, nothing we try (and we’ve tried lots of things!) waits for all the other functions to finish. All our element fields are populating correctly, and we have the intermediate data we want to work on next in the $w(‘#testDedup’).text value ( [“590000016489-1”,“123456789014-1”,“590000015338-4”,“123456789015-4”,“123456789016-3”,“590000003908-1”] ).
How do we cause that hard stop for the next restart?
let dedupOffers = function () {
let set = new Set(allOfferNumbers);
let uniqueArr = Array.from(set);
$w('#testDedup').text = JSON.stringify(uniqueArr);
let countUnique = uniqueArr.length;
$w('#allMyOffers').text = countUnique.toString();
}
let uniqueOffers = function () {
for (let i = 0; i < countUnique; i++) {
let thisItem = uniqueArr[i];
allUniqueOffers.push(thisItem);
$w('#allMyOffers').text = JSON.stringify(allUniqueOffers);
}
$w('#checkDone').text = "ran this function";
$w('#checkDone').text = $w('#testDedup').text;
}
// Async function to get offers by waiting for all getMy___ to complete
// Use 'await Promise.all()' to wait for completion of these functions
async function getMyOffers() {
const [wishListResult, productsResult, brandsResult] = await Promise.all([
await getMyWishList(),
await getMyProducts(),
await getMyBrands(),
]);
uniqueOffers();
}
// Call the async function
getMyOffers();