void
ServiceWorkerJob::InvokeResultCallbacks(ErrorResult& aRv)
{
  AssertIsOnMainThread();
  MOZ_ASSERT(mState == State::Started);

  MOZ_ASSERT(!mResultCallbacksInvoked);
  mResultCallbacksInvoked = true;

  nsTArray<RefPtr<Callback>> callbackList;
  callbackList.SwapElements(mResultCallbackList);

  for (RefPtr<Callback>& callback : callbackList) {
    // The callback might consume an exception on the ErrorResult, so we need
    // to clone in order to maintain the error for the next callback.
    ErrorResult rv;
    aRv.CloneTo(rv);

    callback->JobFinished(this, rv);

    // The callback might not consume the error.
    rv.SuppressException();
  }
}