Skip to content

Instantly share code, notes, and snippets.

@inexorabletash
Last active September 25, 2015 15:51
Show Gist options
  • Save inexorabletash/8c122c84a65de65c35b3 to your computer and use it in GitHub Desktop.
Save inexorabletash/8c122c84a65de65c35b3 to your computer and use it in GitHub Desktop.
Indexed DB + Promises #3
@inexorabletash
Copy link
Author

One option for IDBCursor's advance()/continue() would be to have them return an IDBRequest which then has .promise hanging off of it. The question is: what IDBRequest? Those methods currently reset the readyState of the original IDBRequest used to open the cursor from "done" back to "pending" and fire off new "success" or "error" events.

How about

  • cursor() and advance() return the same IDBRequest originally used to open the cursor (NOTE: I've wanted this in other polyfills!)
  • Generate a new internal, unfulfilled Promise (the one returned by .promise) for the request at the same time as the readyState is reset

So rq.promise === rq.promise would still hold, just not over time. Iteration would then look like:

async function getAll(store, query) {
  let result = [];
  let cursor = await store.openCursor(query).promise;
  while (cursor) {
    result.push(cursor.value);
    cursor = await cursor.continue().promise; // only change
  }
  return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment