Promise.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 <LibJS/SafeFunction.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::WebIDL {
  15. using ReactionSteps = JS::SafeFunction<WebIDL::ExceptionOr<JS::Value>(JS::Value)>;
  16. // https://webidl.spec.whatwg.org/#es-promise
  17. using Promise = JS::PromiseCapability;
  18. JS::NonnullGCPtr<Promise> create_promise(JS::Realm&);
  19. JS::NonnullGCPtr<Promise> create_resolved_promise(JS::Realm&, JS::Value);
  20. JS::NonnullGCPtr<Promise> create_rejected_promise(JS::Realm&, JS::Value);
  21. void resolve_promise(JS::Realm&, Promise const&, JS::Value = JS::js_undefined());
  22. void reject_promise(JS::Realm&, Promise const&, JS::Value);
  23. JS::NonnullGCPtr<JS::Promise> react_to_promise(Promise const&, Optional<ReactionSteps> on_fulfilled_callback, Optional<ReactionSteps> on_rejected_callback);
  24. JS::NonnullGCPtr<JS::Promise> upon_fulfillment(Promise const&, ReactionSteps);
  25. JS::NonnullGCPtr<JS::Promise> upon_rejection(Promise const&, ReactionSteps);
  26. void mark_promise_as_handled(Promise const&);
  27. void wait_for_all(JS::Realm&, Vector<JS::NonnullGCPtr<Promise>> const& promises, Function<void(Vector<JS::Value> const&)> success_steps, Function<void(JS::Value)> failure_steps);
  28. // Non-spec, convenience method.
  29. JS::NonnullGCPtr<JS::Promise> create_rejected_promise_from_exception(JS::Realm&, Exception);
  30. }