I'm using Vanilla javascript for now.
The problem is - I have a IndexedDB where I keep events (let's call them punches). The logic is whenever user punches something, a new record is created in IndexedDB. The record looks like this:
{punchedon: 1687522698067, controlpointid: '1', method: '3', qrcodetext: '4', latitude: '5', ... synchronized: 0}
I have a dedicated field "sychronized" (1/0) in my IndexedDB record, that for each punch carries information whether this particular punch was already synced with server or not.
I have a procedure that is triggered whenever the browser comes back online, that should loop through all records that haven't been synced yet (synchronized=0), send them to the server and IF (AND ONLY IF) the server responds OK, to set synchronized=1.
However I'm struggling with how both IndexedDB and HTTPRequests are asynchronous.
This is the pseudocode I have so far, but I realize it does not work properly (it will update synchronize=1 even if the call to server fails).
    function syncPunch(punch) {
        httpRequest = new XMLHttpRequest();
    
        if (!httpRequest) {
          return false;
        }
        
        httpRequest.onreadystatechange = alertContents;
        httpRequest.open("GET", "https://server/punch.php?controlid=123&teamid=123×tamp=ABC...."); // fake call
        httpRequest.send();
    }
    
    function syncUnsynced() {  // sync all not yet synced punches
      let tx = db.transaction(['punches'], 'readwrite');
      let store = tx.objectStore('punches');
      let syncIndex = store.index('synchronized'); // I have an index on the synchronized field, I query only those synchronized=0
      let keyRng = IDBKeyRange.only(0);  
      
      const cursorRequest = syncIndex.openCursor(keyRng);
      cursorRequest.onsuccess = e => {
        const cursor = e.target.result;
        if (cursor) {
            const punch = cursor.value;
            const punchKey = cursor.value.key;
            
            synchPunch(punch);  // here I need to wait (react on / callback / promise??) to make sure the call was succesfull
            
            console.log("Now fake-syncying " + JSON.stringify(punch));
            punch.synchronized = 1;
            const updateRequest = cursor.update(punch);  // if this update fails and the call was sucesfull, I can live with that. But in normal scenario, I don't want to sync something that has been synced already.
            console.log("Updated " + punchKey);
    
            cursor.continue();
        }
      }
    }
I tried to understand the Promises from the doc, but boy I got lost.
Any help on how to implement this logic appreciated.
 
    