Javascript while loop not running till condition is satisifed in appium+wd.js

I am trying to count the total elements of a recycler view in android appium using wd.js.

For this I am using the logic:

1.For the id get all elements displayed on the screen>>get each elements unique content description and save it to an array.

2.Swipe/scroll the screen >>save the element’s unique attribute to the array>>while total length of the array is not equal to the total count

I have written below code for this:

it(“Scroll and save offerid to offersPresent List”, function () {
while(offersPresent.length !== offersCount) {
return offersPOM.swipeNew(driver, function (err, res) { }).then(function () {
driver.elementsById(‘someId’).then(function (els) {
els.forEach(function (el) {
el.getAttribute(‘contentDescription’).then(function (offerId) {
//Here I check if the offerId is already there in the array or not
if (offersPresent.indexOf(offerId) === -1) {
console.log(‘offersPresent’.green, offersPresent);


The swipNew function is:

exports.swipeNew = function (driver, callback) {
var action = new TouchAction(driver);
.press({ x: 17, y: 1500 })
.moveTo({ x: 17, y: 254 })
return action.perform().then(function (err, res) {
return callback(err, res);
The problem is no matter the condition, this loop only runs once. How to make this loop run till the while condition is satisfied?