Promise.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023, networkException <networkexception@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Runtime/PromiseCapability.h>
  10. #include <LibJS/Runtime/Value.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::WebIDL {
  14. using ReactionSteps = GC::Function<WebIDL::ExceptionOr<JS::Value>(JS::Value)>;
  15. // https://webidl.spec.whatwg.org/#es-promise
  16. using Promise = JS::PromiseCapability;
  17. GC::Ref<Promise> create_promise(JS::Realm&);
  18. GC::Ref<Promise> create_resolved_promise(JS::Realm&, JS::Value);
  19. GC::Ref<Promise> create_rejected_promise(JS::Realm&, JS::Value);
  20. void resolve_promise(JS::Realm&, Promise const&, JS::Value = JS::js_undefined());
  21. void reject_promise(JS::Realm&, Promise const&, JS::Value);
  22. GC::Ref<Promise> react_to_promise(Promise const&, GC::Ptr<ReactionSteps> on_fulfilled_callback, GC::Ptr<ReactionSteps> on_rejected_callback);
  23. GC::Ref<Promise> upon_fulfillment(Promise const&, GC::Ref<ReactionSteps>);
  24. GC::Ref<Promise> upon_rejection(Promise const&, GC::Ref<ReactionSteps>);
  25. void mark_promise_as_handled(Promise const&);
  26. void wait_for_all(JS::Realm&, Vector<GC::Ref<Promise>> const& promises, Function<void(Vector<JS::Value> const&)> success_steps, Function<void(JS::Value)> failure_steps);
  27. // Non-spec, convenience method.
  28. GC::Ref<Promise> create_rejected_promise_from_exception(JS::Realm&, Exception);
  29. }