bool parse_body(JSContext *cx, HandleObject self, UniqueChars buf, size_t len) {
  RootedObject result_promise(cx, &JS::GetReservedSlot(self, Slots::BodyAllPromise).toObject());
  JS::SetReservedSlot(self, Slots::BodyAllPromise, JS::UndefinedValue());
  RootedValue result(cx);

  if (result_type == RequestOrResponse::BodyReadResult::ArrayBuffer) {
    auto *rawBuf = buf.release();
    RootedObject array_buffer(cx, JS::NewArrayBufferWithContents(cx, len, rawBuf));
    if (!array_buffer) {
      JS_free(cx, rawBuf);
      return RejectPromiseWithPendingError(cx, result_promise);
    }
    result.setObject(*array_buffer);
  } else {
    RootedString text(cx, JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(buf.get(), len)));
    if (!text) {
      return RejectPromiseWithPendingError(cx, result_promise);
    }

    if (result_type == RequestOrResponse::BodyReadResult::Text) {
      result.setString(text);
    } else {
      MOZ_ASSERT(result_type == RequestOrResponse::BodyReadResult::JSON);
      if (!JS_ParseJSON(cx, text, &result)) {
        return RejectPromiseWithPendingError(cx, result_promise);
      }
    }
  }

  return JS::ResolvePromise(cx, result_promise, result);
}
bool parse_body(JSContext *cx, HandleObject self, UniqueChars buf, size_t len) {
  RootedObject result_promise(cx, &JS::GetReservedSlot(self, Slots::BodyAllPromise).toObject());
  JS::SetReservedSlot(self, Slots::BodyAllPromise, JS::UndefinedValue());
  RootedValue result(cx);

  if (result_type == RequestOrResponse::BodyReadResult::ArrayBuffer) {
    auto *rawBuf = buf.release();
    RootedObject array_buffer(cx, JS::NewArrayBufferWithContents(cx, len, rawBuf));
    if (!array_buffer) {
      JS_free(cx, rawBuf);
      return RejectPromiseWithPendingError(cx, result_promise);
    }
    result.setObject(*array_buffer);
  } else {
    RootedString text(cx, JS_NewStringCopyUTF8N(cx, JS::UTF8Chars(buf.get(), len)));
    if (!text) {
      return RejectPromiseWithPendingError(cx, result_promise);
    }

    if (result_type == RequestOrResponse::BodyReadResult::Text) {
      result.setString(text);
    } else {
      MOZ_ASSERT(result_type == RequestOrResponse::BodyReadResult::JSON);
      if (!JS_ParseJSON(cx, text, &result)) {
        return RejectPromiseWithPendingError(cx, result_promise);
      }
    }
  }

  return JS::ResolvePromise(cx, result_promise, result);
}