Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Indexeddb: Differences between onsuccess and oncomplete?

I use two different events for the callback to respond when the IndexedDB transaction finishes or is successful:

Let's say... db : IDBDatabase object, tr : IDBTransaction object, os : IDBObjectStore object

tr = db.transaction(os_name,'readwrite');
os = tr.objectStore();

case 1 :

r = os.openCursor();
r.onsuccess = function() {
    if(r.result){
        callback_for_result_fetched();
        r.result.continue;
    } else {
        callback_for_transaction_finish();
    }
}

case 2:

tr.oncomplete = callback_for_transaction_finish();

It is a waste if both of them work similarly. So can you tell me, is there any difference between them?

Answer*

Cancel
3
  • just what i was looking for. i assume objectStore.onsuccess, and objectStore.oncomplete can also be used for multi .add, . get, .put, etc... on given objectstore. Commented Oct 15, 2016 at 7:39
  • do you need trx.commit()? And for reading, if I want to do flatmap from multiple stores, should I do it with onSuccess instead? I have a wrapper function to make all of them async functions.
    – Pui Ho Lam
    Commented Aug 27, 2024 at 18:13
  • 1
    @PuiHoLam commit MAY be called but not MUST to (if called will commit the changes immediately, if not - I believe it'll happen at the end of the current microtask, but see more here. Regarding onSuccess vs onComplete - well, it's up to your requirement, either do it in any case or only in success case.
    – GullerYA
    Commented Aug 28, 2024 at 7:59