jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.29k stars 353 forks source link

Cursor Documentation Unclear #209

Closed stephencweiss closed 3 years ago

stephencweiss commented 3 years ago

Trying to understand how to use cursors - specifically whether or not we need to await the delete() call:

clearExpired: async (now: string) => {
      const transaction = database.transaction('acknowledgements', 'readwrite')
      const cursor = await transaction
        .store.index('modified')
        .openCursor(IDBKeyRange.upperBound(now, true));
      while (cursor) {
        cursor.delete() // TODO: Do we need to await this?
        await cursor.continue()
      }

      await transaction.done;
    }

The goal of this function is to evaluate against a now string and use a cursor to iterate over our index (modified - which is also a string). If the value in modified is before (non-inclusive) - we would delete.

Reading over the documentation I was confused about the appropriate way to ensure this behavior.

Thanks in advance!

jakearchibald commented 3 years ago

The only reason you may need to await particular parts of a transaction is to get the return value or catch failure.

In this case there's no return value. You might catch a failure, but if that happens the whole transaction will fail, so you might not be interested in the individual failure anyway.

TL;DR nah you don't need to await it.