how to make synchronous call to indexeddb method from javascript
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
IndexedDB is asynchronous by design — there is no way to make a truly synchronous call to it from the main thread. The browser intentionally prevents synchronous database access to avoid blocking the UI. The solution is to use Promises and async/await to write code that reads like synchronous code but executes asynchronously. If you genuinely need synchronous IndexedDB access, it was available in Web Workers via indexedDB.openSync() in an early spec, but no browser ever implemented it.
Why IndexedDB Is Async-Only
IndexedDB operations involve disk I/O. A synchronous disk read on the main thread would freeze the page — no scrolling, no animations, no click handlers — until the read completes. This can take 1-100ms or more. The browser API forces asynchronous access to guarantee a responsive UI.
Wrapping IndexedDB in Promises
Convert the callback-based API into Promises:
Using async/await (Looks Synchronous)
This code is asynchronous under the hood but reads top-to-bottom like synchronous code.
Using the idb Library (Recommended)
The idb library by Jake Archibald wraps IndexedDB with Promises automatically:
Install with npm install idb. It is only 1.2KB gzipped.
Cursor Iteration with Promises
Pre-Loading Data for Synchronous Access
If you need synchronous reads in a hot path, load data into memory first:
Common Pitfalls
- Expecting synchronous access: No browser supports synchronous IndexedDB on the main thread. The
IDBFactory.openSync()from the early spec was never implemented. Do not look for it. - Not awaiting transactions: IndexedDB transactions auto-commit when all requests are done. If you start a transaction and do async work before using it, the transaction may already be committed. Keep all operations within the same microtask.
- Error handling: IndexedDB errors propagate via
onerrorcallbacks, not exceptions. Without Promise wrappers, errors are silently lost. Always add error handlers or use theidblibrary. - Version management:
indexedDB.open(name, version)only triggersonupgradeneededwhen the version increases. Forgetting to bump the version means schema changes are silently ignored. - Blocking the page with large reads: Even async reads can cause jank if you process millions of records in a tight loop. Use cursors with
requestAnimationFrameor Web Workers for large datasets.
Summary
- IndexedDB has no synchronous API on the main thread — this is by design
- Wrap IndexedDB requests in Promises and use
async/awaitfor synchronous-looking code - Use the
idblibrary for a clean, Promise-based IndexedDB API - For truly synchronous access, pre-load data into a memory cache
- Always handle errors — IndexedDB silently drops errors without proper handlers
Related reading
- how to make synchronous http calls within async.each in nodejs
- How to make this asynchronous? async, await - C, MVC
- How to making async calls to Amazon Bedrock
- How to manage a mutex in an asynchronous method
- How to make the corners of a button round?
- How to make Twitter Bootstrap menu dropdown on hover rather than click
- how to manage an NDC-like log4net stack with async/await methods? per-Task stack?
- How to manage multiple Async Tasks efficiently in Android
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.