# If vs. Do with Promise

**URL:** <https://forum.wixstudio.com/t/if-vs-do-with-promise/34236>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [May 9, 2018, 5:10pm UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236 "2018-05-09T17:10:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [May 9, 2018, 5:10pm UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236/1 "2018-05-09T17:10:26Z")

</div>

I have pinpointed my [problem](https://www.wix.com/code/home/forum/questions-answers/promises-promises-again) to a different behaviour between IF and DO/while.  
In the below code, the function chkIfExist(strRandomNum) if perfectly executed and returns a new number.  
But, if I replace the IF {} with a DO {} while (remmed out), the chkIfExist(strRandomNum) function is called, but does not go into the query and does not return a result (resulting in a never ending loop).

Can anybody help me remedy this? I am clueless.

```javascript
import wixData from 'wix-data';

export function button1_click(event, $w) {
	wixData.query("Vouchers")
		.eq("fldVoucherId", "414722")
		.count()
		.then((num) => {
			let numberOfItems = num;
			console.log("numberOfItems=" + numberOfItems);
// here is the do
// do {
			if (numberOfItems > 0) {
				let newNum = getRandomNum(100000, 999999).toString();
				chkIfExist(newNum)
					.then((amount) => {
						numberOfItems = amount;
						console.log("numberOfItems=" + numberOfItems);
					});
			}
// and while
// while (numberOfItems > 0);
		});
}

function getRandomNum(min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function chkIfExist(strRandomNum) {
	console.log("3: In chkIfExist");
	console.log("4: strRandomNum before query= " + strRandomNum);
	return wixData.query("Vouchers")
		.eq("fldVoucherId", strRandomNum)
		.count()
		.then((amount) => {
			let numCount = amount;
			console.log("5: numCount=" + numCount);
			let boolIdExists = (numCount > 0);
			return numCount;
		})
		.catch((err) => {
			let errorMsg = err;
			console.log("errorMsg=" + errorMsg);
		});

}

```

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [May 10, 2018, 7:07am UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236/2 "2018-05-10T07:07:51Z")

</div>

Hey Giri,

You know what that example did to me? 🤯

Seriously, the issue that you’re having is that the variable **numberOfItems** gets changed in the **.then** (that is, when the promise has been fulfilled), but it is being inspected outside of the **.then**. I get a headache just thinking about it.

You’ll have to adjust your logic to make sure that **numberOfItems** is available at the correct time and isn’t waiting for its new value.

Good luck. 🙃

Yisrael

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [May 10, 2018, 4:24pm UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236/3 "2018-05-10T16:24:14Z")

</div>

"You know what that example did to me? "  
I have got an inkling, Yisrael : I could have blow dried my hair on the combination of an overheating CPU and a ventilator spinning like mad. But maybe you refer you the rolling of your eyeballs?

To prevent my CPU burning a hole in my desk, could you indicate what you mean by the inspection and where (is it the do-loop), because, to be boringly honest, I don´t have a clue and the fun of reloading the editor every time starts to get to me after a while.  
Appreciate your help, as I admitted earlier, those Promises, promises, they are not my strong point (euphemism of the day).

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [May 13, 2018, 8:09am UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236/4 "2018-05-13T08:09:54Z")

</div>

Hey Giri,

🙃 nice to start the day off laughing…

OK - enough frivolity…

Your problem is not trivial, but let me attempt to break it down into pieces that I can almost understand if I think hard enough.

You will first need this routine. Using async/await is a little easier in this context:

```javascript
async function chkIfExist(strRandomNum) {
    console.log("3: In chkIfExist");
    console.log("4: strRandomNum before query= " + strRandomNum);
    const amount = await wixData.query("Vouchers")
       .eq("fldVoucherId", strRandomNum)
       .count();
    return amount;
}

```

Then, redo your loop to look something like this:

```javascript
do {
   let newNum = getRandomNum(100000, 999999).toString(); 
   numberOfItems = await chkIfExist(newNum);
   console.log("numberOfItems=" + numberOfItems);
while (numberOfItems > 0);

```

Disclaimer: I have not tested this code. It is only intended as a possible scenario that might work if you’re dreaming or have had enough beer. This might melt your desk or short out the power on your city block. It is not my fault. You have been warned.

Try this out, making the necessary adjustments for any errors I may have made in the above code snippets. You can read more about async/await in Wix Code to help you understand this better.

If you’re not able to work this out, please post the URL of your site so I can have the opportunity to melt my desk. Or my mind.

Yisrael

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [May 14, 2018, 3:12pm UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236/5 "2018-05-14T15:12:06Z")

</div>

Yisrael, I had come to the same conclusion. I mistakenly assumed .then and async/await to be equivalent. I now think they can be (if there is no more code after a .then), but they don´t have to be. .then continues executing code after a .then (in my example the do-loop). Async/await really stops executing any other code UNTIL the promise is resolved. I read that article some 5 times, and it is getting clearer to me now. Thanks for your help.  
If anyone runs into the same problem, the working code looks like this:

```javascript
import wixData from 'wix-data';

export async function button1_click(event, $w) {
	let uniqueVoucher = await getUniqueVoucherId();
console.log("uniqueVoucher main=" + uniqueVoucher);
}

async function getUniqueVoucherId() {
	let numberOfItems;
	let newNum;
	do {
		newNum = getRandomNum(100000, 999999).toString();
		numberOfItems = await chkIfExist(newNum);
console.log("numberOfItems 2 (while)=" + numberOfItems);
	}
	while (numberOfItems > 0);
	return newNum;
}

function getRandomNum(min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

async function chkIfExist(strRandomNum) {
	console.log("3: In chkIfExist");
	console.log("4: strRandomNum before query= " + strRandomNum);
	const amount = await wixData.query("Vouchers")
		.eq("fldVoucherId", strRandomNum)
		.count();
console.log("amount in chkIfExist= " + amount);
	return amount;
}

```

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [May 14, 2018, 4:27pm UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236/6 "2018-05-14T16:27:59Z")

</div>

Giri,

Yay! 🎉 Glad you got that worked out. Ya know, we gotta have a beer together someday. Maybe several.

Yisrael 🍻

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [May 14, 2018, 10:17pm UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236/7 "2018-05-14T22:17:21Z")

</div>

Best idea since ages! Do prefer g&t over beer, but for you I´ll make an exception. Small detail: there´s an ocean dividing us.

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [May 15, 2018, 5:00am UTC](https://forum.wixstudio.com/t/if-vs-do-with-promise/34236/8 "2018-05-15T05:00:53Z")

</div>

One never knows Giri.
