I don't understand asynchronous communication!!!!

I need help with this:
I have a table that shows the data from a collection. I need to delete this data and then refresh the table so this show without any register, So I wrote the code shown.
The problem is that the table refreshes before that the deletion has finished, so I see some of the records still in the table.
(Note that I really don’t refresh the table but I refresh it’s dataset)
Please:

  1. What can I do to avoid that the refresh begins until ALL deletion has finished?
  2. The deletion has to do in a loop? Is no way to make in a one step procedure?

This is the code:

export function buttonBorrarResultado_click(event)
{
console.log (“1. Begin deletion”)
wixData.query(“TempPeople”)
.limit(1000)
.find()
.then((result) => {
for ( var i = 0; i < result.items.length; i++){
if (result.items[i] === null ) continue ;
wixData.remove(“TempPeople”, result.items[i]._id);
}
console.log (“2. end deletetion”)

   console.log ("3. ibegin refresh") 
           $w("#datasetTempPeople").refresh(); 
    console.log('4. endrefresh'); 
}); 

}

In order to wait for each operation to finish, you will need to use Promises. For a gentle introduction see the post Promises, Promise . For a more complete treatment of Promises, see the article Wix Code: Working with Promises .

Your query is already using a Promise since the .then() function handles the Promise fulfillment. The remove() should have a .then() function, or use await. Something like this should work:

await wixData.remove("TempPeople", result.items[i]._id);

In this way, the refresh() will only execute after all calls to remove() in the loop are finished.

I hope this helps,

Yisrael

@Yisrael: Thank you! It worked!!!
But I still have the second question: The deletion has to do in a loop? Is no way to make in a one step procedure?
Can you comment about this point?
Arturo

@avillarg You have to loop through the records you want to delete to get their _id accordingly. There is no deleteAll() function even if I would love that

@andreas-kviby : Thank you! Yes, it should be great to have this function!

@avillarg Andreas is correct - I missed that. Right now, I do my own deleteAll():

wixData.query("NewCollectionName”)
  .limit(1000)
  .find()
  .then((result) => {
    for (var i = 0; i < result.items.length; i++){
      if (result.items[i] === null) continue;
      wixData.remove("NewCollectionName", result.items[i]._id);
    }
    console.log('erase done');
  });

I hope that helps.

@yisrael-wix : Great! Thank you!

@yisrael-wix : I cannot see my other posts! I have made some questions and I cannot find them. In my profile I see some statistics about them but is not possible to see the post. An in MyAccount produces an error.
Can you help me with this issue?
Arturo

@avillarg From what I understand, there’s currently an issue with the forum and it’s being worked on. Sorry for any inconvenience.