bool process_pending_requests(JSContext *cx) {
  if (pending_requests->length() == 0)
    return true;

  size_t count = pending_requests->length();
  auto handles = mozilla::MakeUnique<PendingRequestHandle[]>(sizeof(PendingRequestHandle) * count);
  if (!handles)
    return false;

  for (size_t i = 0; i < count; i++) {
    handles[i] = Request::pending_handle((*pending_requests)[i]);
  }

  uint32_t done_index;
  ResponseHandle response_handle = {INVALID_HANDLE};
  BodyHandle body = {INVALID_HANDLE};
  int result =
      xqd_req_pending_req_select(handles.get(), count, &done_index, &response_handle, &body);
  if (!HANDLE_RESULT(cx, result))
    return false;

  HandleObject request = (*pending_requests)[done_index];
  RootedObject response_promise(cx, Request::response_promise(request));

  if (response_handle.handle == INVALID_HANDLE) {
    pending_requests->erase(const_cast<JSObject **>(request.address()));
    JS_ReportErrorUTF8(cx, "NetworkError when attempting to fetch resource.");
    RootedValue exn(cx);
    if (!JS_IsExceptionPending(cx) || !JS_GetPendingException(cx, &exn)) {
      return false;
    }
    JS_ClearPendingException(cx);
    return JS::RejectPromise(cx, response_promise, exn);
  }

  RootedObject response_instance(
      cx, JS_NewObjectWithGivenProto(cx, &Response::class_, Response::proto_obj));
  if (!response_instance)
    return false;

  RootedObject response(cx, Response::create(cx, response_instance, response_handle, body, true));
  if (!response)
    return false;

  RequestOrResponse::set_url(response, RequestOrResponse::url(request));
  RootedValue response_val(cx, JS::ObjectValue(*response));

  pending_requests->erase(const_cast<JSObject **>(request.address()));

  return JS::ResolvePromise(cx, response_promise, response_val);
}
bool process_pending_requests(JSContext *cx) {
  if (pending_requests->length() == 0)
    return true;

  size_t count = pending_requests->length();
  auto handles = mozilla::MakeUnique<PendingRequestHandle[]>(sizeof(PendingRequestHandle) * count);
  if (!handles)
    return false;

  for (size_t i = 0; i < count; i++) {
    handles[i] = Request::pending_handle((*pending_requests)[i]);
  }

  uint32_t done_index;
  ResponseHandle response_handle = {INVALID_HANDLE};
  BodyHandle body = {INVALID_HANDLE};
  int result =
      xqd_req_pending_req_select(handles.get(), count, &done_index, &response_handle, &body);
  if (!HANDLE_RESULT(cx, result))
    return false;

  HandleObject request = (*pending_requests)[done_index];
  RootedObject response_promise(cx, Request::response_promise(request));

  if (response_handle.handle == INVALID_HANDLE) {
    pending_requests->erase(const_cast<JSObject **>(request.address()));
    JS_ReportErrorUTF8(cx, "NetworkError when attempting to fetch resource.");
    RootedValue exn(cx);
    if (!JS_IsExceptionPending(cx) || !JS_GetPendingException(cx, &exn)) {
      return false;
    }
    JS_ClearPendingException(cx);
    return JS::RejectPromise(cx, response_promise, exn);
  }

  RootedObject response_instance(
      cx, JS_NewObjectWithGivenProto(cx, &Response::class_, Response::proto_obj));
  if (!response_instance)
    return false;

  RootedObject response(cx, Response::create(cx, response_instance, response_handle, body, true));
  if (!response)
    return false;

  RequestOrResponse::set_url(response, RequestOrResponse::url(request));
  RootedValue response_val(cx, JS::ObjectValue(*response));

  pending_requests->erase(const_cast<JSObject **>(request.address()));

  return JS::ResolvePromise(cx, response_promise, response_val);
}