4.1. The IDBRequest interface
The [IDBRequest](#idbrequest)
interface provides the means to access results of asynchronous requests to databases and database objects using event handler IDL attributes [HTML52].
Every method for making asynchronous requests returns an [IDBRequest](#idbrequest)
object that communicates back to the requesting application through events. This design means that any number of requests can be active on any database at a time.
In the following example, we open a database asynchronously. Various event handlers are registered for responding to various situations.
- var request = indexedDB.open('AddressBook', 15);
- request.onsuccess = function(evt) {...};
- request.onerror = function(evt) {...};
- [Exposed=(Window,Worker)]
- interface
IDBRequest
: EventTarget {- readonly attribute any result;
- readonly attribute DOMException? error;
- readonly attribute (IDBObjectStore or IDBIndex or IDBCursor)? source;
- readonly attribute IDBTransaction? transaction;
- readonly attribute IDBRequestReadyState readyState;
- // Event handlers:
- attribute EventHandler onsuccess;
- attribute EventHandler onerror;
- };
- enum
IDBRequestReadyState
{"pending"
,"done"
- };
request . [result](#dom-idbrequest-result)
When a request is completed, returns the result, or undefined
if the request failed. Throws a “[InvalidStateError](https://www.w3.org/TR/WebIDL-1/#invalidstateerror)
“ [DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
if the request is still pending.
request . [error](#dom-idbrequest-error)
When a request is completed, returns the error (a [DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
), or null if the request succeeded. Throws a “[InvalidStateError](https://www.w3.org/TR/WebIDL-1/#invalidstateerror)
“ [DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
if the request is still pending.
request . [source](#dom-idbrequest-source)
Returns the [IDBObjectStore](#idbobjectstore)
, [IDBIndex](#idbindex)
, or [IDBCursor](#idbcursor)
the request was made against, or null if is was an open request.
request . [transaction](#dom-idbrequest-transaction)
Returns the [IDBTransaction](#idbtransaction)
the request was made within. If this as an open request, then it returns an upgrade transaction while it is running, or null otherwise.
request . [readyState](#dom-idbrequest-readystate)
Returns ["pending"](#dom-idbrequestreadystate-pending)
until a request is complete, then returns ["done"](#dom-idbrequestreadystate-done)
.
The result
attribute’s getter must throw an “[InvalidStateError](https://www.w3.org/TR/WebIDL-1/#invalidstateerror)
“ [DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
if the done flag is unset. Otherwise, the attribute’s getter must return the result of the request, or undefined if the request resulted in an error.
The error
attribute’s getter must throw an “[InvalidStateError](https://www.w3.org/TR/WebIDL-1/#invalidstateerror)
“ [DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException)
if the done flag is unset. Otherwise, the attribute’s getter must return the error of the request, or null if no error occurred.
The source
attribute’s getter must return the source of the request, or null if no source is set.
The transaction
attribute’s getter must return the transaction of the request. This property can be null for certain requests, such as for requests returned from [open()](#dom-idbfactory-open)
.
The readyState
attribute’s getter must return ["pending"](#dom-idbrequestreadystate-pending)
if the done flag is unset, and ["done"](#dom-idbrequestreadystate-done)
otherwise.
The onsuccess
attribute is the event handler for the success
event.
The onerror
attribute is the event handler for the error
event.
Methods on [IDBDatabase](#idbdatabase)
that return a open request use an extended interface to allow listening to the [blocked](#request-blocked)
event and [upgradeneeded](#request-upgradeneeded)
event.
- [Exposed=(Window,Worker)]
- interface
IDBOpenDBRequest
: IDBRequest {- // Event handlers:
- attribute EventHandler onblocked;
- attribute EventHandler onupgradeneeded;
- };
The onblocked
attribute is the event handler for the [blocked](#request-blocked)
event.
The onupgradeneeded
attribute is the event handler for the [upgradeneeded](#request-upgradeneeded)
event.