5.6. Asynchronously executing a request

The steps to asynchronously execute a request are as follows. The algorithm takes a source object and an operation to perform on a database, and an optional request.

These steps can be aborted at any point if the transaction the created request belongs to is aborted using the steps to abort a transaction.

  1. Let transaction be the transaction associated with source.

  2. Assert: transaction is active.

  3. If request was not given, let request be a new request with source as source.

  4. Add request to the end of transaction’s request list.

  5. Run these steps in parallel:

    1. Wait until all previously added requests in transaction have their done flag set.

    2. Let result be the result of performing operation.

    3. If result is an error, then revert all changes made by operation.

      This only reverts the changes done by this request, not any other changes made by the transaction.

    4. Queue a task to run these steps:

      1. Set the done flag on request.

      2. If result is an error, then:

        1. Set the result of request to undefined.

        2. Set the error of request to result.

        3. Fire an error event at request.

      3. Otherwise:

        1. Set the result of request to result.

        2. Set the error of request to undefined.

        3. Fire a success event at request.

  6. Return request.