@Aleksei Thanks for your help. I was able to figure it out. The problem was indeed of RETURN.
Instead of while loop I chose the recursive implementation as while loop is not recommended for async function calls in javascript.
let swipeAndSaveElement = async function (driver, offersDisplayedInApp, offersCountFromApi) {
let els = await driver.elementsById(offersBanner);
if (els.length > 0) {
var repeatedIdCount = 0
for (let el of els) {
let offerId = await el.getAttribute('contentDescription');
if (offersDisplayedInApp.indexOf(offerId) === -1) {
offersDisplayedInApp.push(offerId);
} else {
repeatedIdCount++;
console.log('offerId already exists in the array'.green);
}
}
if (offersDisplayedInApp.length <= offersCountFromApi && repeatedIdCount < 3) {
await swipeNew(driver);
await swipeAndSaveElement(driver, offersDisplayedInApp, offersCountFromApi);
}
else if (offersDisplayedInApp.length === offersCountFromApi) {
console.log('OffersId count matched'.green);
return true;
} else {
throw "offersId count didn't match with the offersCountFromApi";
return false;
}
} else {
console.log('offerIds not found'.red);
return false;
}
}