stedrolln34
stedrolln34

Reputation: 147

indexedDB idb library - insert multiple records

Using javascript Promises, i'm using the idb library to store data in indexedDB. I am trying to figure out how to guarantee multiple 'adds' will be committed successfully before the transaction ends.

My question is which of the following is correct with regards to adding multiple records within one transaction:

var store = transaction.objectStore(...);
for(var i=0; i<records.length; i++) { store.add(records[i]); }
return transaction.complete;

or

var store = transaction.objectStore(...);
return Promise.all(records.map( record => { return store.add(record); }))
   .then( function () { return transaction.complete; });

which one guarantees all records will be added successfully before the transaction ends? why?

Upvotes: 1

Views: 1338

Answers (1)

stedrolln34
stedrolln34

Reputation: 147

So based on the comments it seems like they are both would work. Since Promise.all isn't necessary seems like the first is easier to read. Thanks.

Upvotes: 1

Related Questions