Dataset Refresh gives an error but no way to catch it

I have some code where a user can remove items from a repeater. When each item is removed I refresh the dataset to refresh the display. When the last item gets removed, the refresh operation gives an error message. There is no way to catch that error message in the code.

async function librarianDataUpdate () {
$w ( ‘#requestsDS’ ). refresh ()
. then (( status) => {
console . log ( 'librarianDataUpdate refresh #requestsDS status: ’ , status );
})
. catch (( err) => {
console . log ( 'librarianDataUpdate refresh #requestsDS error: ’ , err );
});
let count = await openRequestCount ();
console . log ( ‘librarianDataUpdate: requestDS count =’ , count );
}

The output I get looks like this. Looks like it never enters the .catch block.

librarianDataUpdate refresh #requestsDS status: undefined app.js:25:18323
librarianDataUpdate: requestDS count = 2 app.js:25:18323
librarianDataUpdate: requestDS count = 1 app.js:25:18323
librarianDataUpdate refresh #requestsDS status: undefined app.js:25:18323

An error occurred in one of currentIndexChanged callbacks
Object { code: “DS_OPERATION_FAILED” }
​code: “DS_OPERATION_FAILED”
: Object { … } app.js:6:14082

librarianDataUpdate refresh #requestsDS status: undefined app.js:25:18323
librarianDataUpdate: requestDS count = 0 app.js:25:18323

Note that in your code, openRequestCount() runs before the refresh is completed. You need to move that code to the refresh’s .then() , something like this:

async function librarianDataUpdate() {
  $w('#requestsDS').refresh()
  .then((status) => {
    console.log('librarianDataUpdate refresh #requestsDS status: ',  status);
    let count = await openRequestCount();
    console.log('librarianDataUpdate: requestDS count =',  count);            
  })
  .catch((err) => {
    console.log('librarianDataUpdate refresh #requestsDS error: ',  err);
  });
}

Note that this has not been tested, but the idea is that you can only get the count after the refresh has completed, and that happens when the Promise is fulfilled.

See the following for more information about Promises:

  • Promises, Promises

  • Velo: Working with Promises