Check out a free preview of the full Progressive Web Applications and Offline course:
The "IndexedDB & Promises" Lesson is part of the full, Progressive Web Applications and Offline course featured in this preview video. Here's what you'd learn in this lesson:

Mike reviews IDB, an open source library that replaces the IDBRequest objects with promises.

Get Unlimited Access Now

Transcript from the "IndexedDB & Promises" Lesson

>> Mike North: And finally, hopefully, you're a little bit terrified by the syntax I showed you, this is pretty miserable stuff to work with. There is a library called IDB and it is written by a Chrome developer advocate, Jay. It's basically, conceptually, an identical API, except it's all promise-based. So we can use promises instead of all of these strange decades old callback style of doing things.

[00:00:36] here's an example of us opening a database and then passing in this upgradeDb callback, that's gonna be the same as onupgradeneeded. You're gonna get the same argument passed to it. It's an attempt to really preserve the API, but just to make it promisified. So it's a super lightweight library.

[00:00:57] And honestly, I would advise you not try to use IndexedDb by itself. Don't do it, use this. There's another good one called local forage, which can use IndexedDb or local storage. But it abstracts the IndexedDb concept away from you a little bit, because it's just designed differently with different goals.

[00:01:23] But not a bad library to use if it meets your needs. Here, you can see that we get this promise from opening the database. We have this callback for upgrading the database. And then by the time the upgrade is complete, the promise will resolve, and then we get this db object in this second block of code here.

[00:01:45] We can create a transaction, do some stuff as it relates to one of these object stores, and then complete the transaction and the operation will finish.