Want to increase code efficiency?

Hey guys,

I have 2 functions and 1 onClick function here.

When I use the 2 function in onClick function without setTimeout event then the functions don’t work properly together. Actually, some value returns faster then some other, hence, it sets null values to the variable . Therefore, when I use them (setTimeout event), it wastes my some seconds.

MY QUESTION IS - HOW CAN I SAVE MY TIME?

CODE HERE-

export function button8_click(event) {
   follow1 = followup1($w("#dropdown1").value);
   setTimeout(() => {
      book1 = booking1($w("#dropdown1").value);
      setTimeout(() => {
         total1 = follow1 + book1;
         $w("#total1").text = "" + total1;
      },1700)     
    },1700)
}

export function booking1(branch)
   $w("#dataset1").setFilter(wixData.filter()
   .contains("branch", branch)
   .contains("remarks", "Booking")
   .then(() => {
      book1 = $w("#dataset1").getTotalCount();
      $w("#book1").text = "" + book1;
      return book1;
   });  
}

export function followup1(branch){
   $w("#dataset1").setFilter(wixData.filter()
   .contains("branch", branch)
   .isEmpty("remarks")
   .or(wixData.filter().contains("remarks", "Service Due")
   .or(wixData.filter().contains("remarks", "Phone Not Pick")
   .or(wixData.filter().contains("remarks", "Mobile No Updated"))))
   .between("nextServiceDate", startDate, endDate))
   .then(() => {
      follow1 = $w("#dataset1").getTotalCount();
      $w("#follow1").text = "" + follow1;
      return follow1;
   });
}

THANKS IN ADVANCE.

Pls guide me further.

It’s not so clear what you’re trying to do, and the way you’re using the book1 variable is unclear.
Anyway, of course you don;t need a timeout in such functions.

But you need to make the first function asynchronous and to use await for the promises.

For example:

export async function button8_click(event) {
   follow1 = await followup1($w("#dropdown1").value);
     book1 = await  booking1($w("#dropdown1").value);
        total1 = follow1 + book1;
        $w("#total1").text = "" + total1;
}

Or even better:

export function button8_click(event) {
Promise.all([
followup1($w("#dropdown1").value),
 booking1($w("#dropdown1").value)
]).then(r => {
total1 = r[0] + r[1];
 $w("#total1").text = "" + total1;
})
}

@jonatandor35 Thanks for your your reply and would tell you soon. :smiley:

Sorry for being late to reply.
I don’t know why but it doesn’t seems to work.

You was asking about my question more clearly, here it is-

When I use two function together they shows same value in console but when I use them a bit later (using setTimeout) they works properly i.e they shows different values.

So of course my code is wasting time and I want to improve its efficiency.

Doesn’t work properly-

export function button8_click(event) {
   follow1 = followup1($w("#dropdown1").value);
   book1 = booking1($w("#dropdown1").value);
   total1 = follow1 + book1;
   $w("#total1").text = "" + total1;
}

Works Properly -

export function button8_click(event) {
   follow1 = followup1($w("#dropdown1").value);
   setTimeout(() => {
      book1 = booking1($w("#dropdown1").value);
      setTimeout(() => {
         total1 = follow1 + book1;
         $w("#total1").text = "" + total1;
      },1700)     
    },1700)
}

Both Functions are written it the Post Question (at the top).

J.D.'s explanation is precisely the problem that you are having. The queries take time and it’s best to handle the Promise and not to rely on a timer.

The following articles will shed some light on what Promises are, why they’re needed, and how to use them. I promise.

Once you understand Promises, everything will become much easier.

Thanks @yisrael-wix
Will see!!:grinning: