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 [HTML].
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.
const 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://heycam.github.io/webidl/#invalidstateerror)
“ [DOMException](https://heycam.github.io/webidl/#idl-DOMException)
if the request is still pending.
request . [error](#dom-idbrequest-error)
When a request is completed, returns the error (a [DOMException](https://heycam.github.io/webidl/#idl-DOMException)
), or null if the request succeeded. Throws a “[InvalidStateError](https://heycam.github.io/webidl/#invalidstateerror)
“ [DOMException](https://heycam.github.io/webidl/#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 it 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://heycam.github.io/webidl/#invalidstateerror)
“ [DOMException](https://heycam.github.io/webidl/#idl-DOMException)
if this‘s done flag is false. Otherwise, the attribute’s getter must return this‘s result, or undefined if the request resulted in an error.
The error
attribute’s getter must throw an “[InvalidStateError](https://heycam.github.io/webidl/#invalidstateerror)
“ [DOMException](https://heycam.github.io/webidl/#idl-DOMException)
if this‘s done flag is false. Otherwise, the attribute’s getter must return this‘s error, or null if no error occurred.
The source
attribute’s getter must return this‘s source, or null if no source is set.
The transaction
attribute’s getter must return this‘s transaction. 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 this‘s done flag is false, 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
and upgradeneeded
events.
- [
Exposed =(Window ,Worker )]interface :
IDBOpenDBRequest IDBRequest {- // Event handlers:
attribute EventHandler onblocked ;attribute EventHandler onupgradeneeded ;- };
The onblocked
attribute is the event handler for the blocked
event.
The onupgradeneeded
attribute is the event handler for the upgradeneeded
event.