14

The indexedDB has a spec saying that you can access an indexed database synchronously, but it hasn't been implemented yet.

I was just wondering if there is a way to make it synchronous manually,

My JavaScript looks like this,

var trans = databaseAsync.transaction(["mapTile"], IDBTransaction.READ_WRITE);
var store = trans.objectStore("mapTile");
var keyRange = IDBKeyRange.bound(evt.data[0], evt.data[0]);
var cursorRequest = store.openCursor(keyRange);

// can you put some kind of wait in here?

cursorRequest.onsuccess = function(e)
{
    var result = e.target.result;
    if(!!result == false)
    {
    }
}

So can you put something in there to make it wait until the onsuccess method has been called before continuing on?

The reason why I want to do this is the code above is wrapped inside this method,

dojo.extend(esri.layers.ArcGISTiledMapServiceLayer, {
      getTileUrl : function(level, row, col)
      {
          // blah
          return url;
      }

So it is an ESRI tile layer (which will load tiles onto a map on my web page), and that method needs to return the url straight away for a particular tile. It will either be a URL to load the image from if it isn't cached in the database already, or this,

data:image;base64,*BASE64DATA*

Where BASE64DATA is the data from the database if previously cached.

I was previously using the localStorage for this, which works synchronously, but that has a 5MB limit so I thought I would experiment with indexedDB.

Josh
  • 17,834
  • 7
  • 50
  • 68
peter
  • 13,009
  • 22
  • 82
  • 142

4 Answers4

3

IndexedDB Sync API is marked as a risky part of the IndexedDB specification and they might be removed due to potential lack of implementations.

I've implemented 'sync' solution using the 'oncomplete' transaction event which guaranties that the current action is finished before starting the next one, and I also use custom semaphore and queue logic which handles the async calls from GUI and ensures that 2 open connections towards IndexedDB database won't happen at the same time.

Deni Spasovski
  • 4,004
  • 3
  • 35
  • 52
3

An elegant way of doing this is provided in the accepted answer to this question I asked a couple months ago. Unfortunately, it relies on feature of JavaScript 1.7 (generators) that is only supported in Firefox. Hopefully some day this technique will be more widely supported.

Community
  • 1
  • 1
dumbmatter
  • 9,351
  • 7
  • 41
  • 80
2

There is a more general question how to wait for asynchronous functions where you will find an answer: How to block on asynchronous functions in JavaScript

I also found a nice waitFor routine inside Trial Tool.

Community
  • 1
  • 1
toka
  • 128
  • 7
0

IE10 supports the sync api, but the indexeddb sync api can only be used inside a web worker. If you are working in the UI thread you need to use the async API.

Easiest way to work with the async API is through promises.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
Kristof Degrave
  • 4,142
  • 22
  • 32