mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibWeb: Implement an AO to get a promise to wait for promises to settle
This commit is contained in:
parent
33623b8353
commit
5074355f29
2 changed files with 33 additions and 0 deletions
|
@ -7,11 +7,13 @@
|
|||
#include <AK/Function.h>
|
||||
#include <AK/TypeCasts.h>
|
||||
#include <LibGC/Function.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/PromiseCapability.h>
|
||||
#include <LibJS/Runtime/PromiseConstructor.h>
|
||||
#include <LibJS/Runtime/Realm.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
|
||||
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
|
||||
#include <LibWeb/WebIDL/Promise.h>
|
||||
|
||||
namespace Web::WebIDL {
|
||||
|
@ -290,6 +292,36 @@ void wait_for_all(JS::Realm& realm, Vector<GC::Ref<Promise>> const& promises, Fu
|
|||
}
|
||||
}
|
||||
|
||||
// https://webidl.spec.whatwg.org/#waiting-for-all-promise
|
||||
GC::Ref<Promise> get_promise_for_wait_for_all(JS::Realm& realm, Vector<GC::Ref<Promise>> const& promises)
|
||||
{
|
||||
// 1. Let promise be a new promise of type Promise<sequence<T>> in realm.
|
||||
auto promise = create_promise(realm);
|
||||
|
||||
// 2. Let successSteps be the following steps, given results:
|
||||
auto success_steps = [&realm, promise](Vector<JS::Value> const& results) {
|
||||
HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
|
||||
|
||||
// 1. Resolve promise with results.
|
||||
auto sequence = JS::Array::create_from(realm, results);
|
||||
resolve_promise(realm, promise, sequence);
|
||||
};
|
||||
|
||||
// 3. Let failureSteps be the following steps, given reason:
|
||||
auto failure_steps = [&realm, promise](JS::Value reason) {
|
||||
HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
|
||||
|
||||
// 1. Reject promise with reason.
|
||||
reject_promise(realm, promise, reason);
|
||||
};
|
||||
|
||||
// 4. Wait for all with promises, given successSteps and failureSteps.
|
||||
wait_for_all(realm, promises, move(success_steps), move(failure_steps));
|
||||
|
||||
// 5. Return promise.
|
||||
return promise;
|
||||
}
|
||||
|
||||
GC::Ref<Promise> create_rejected_promise_from_exception(JS::Realm& realm, Exception exception)
|
||||
{
|
||||
auto throw_completion = Bindings::exception_to_throw_completion(realm.vm(), move(exception));
|
||||
|
|
|
@ -30,6 +30,7 @@ GC::Ref<Promise> upon_fulfillment(Promise const&, GC::Ref<ReactionSteps>);
|
|||
GC::Ref<Promise> upon_rejection(Promise const&, GC::Ref<ReactionSteps>);
|
||||
void mark_promise_as_handled(Promise const&);
|
||||
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);
|
||||
GC::Ref<Promise> get_promise_for_wait_for_all(JS::Realm&, Vector<GC::Ref<Promise>> const& promises);
|
||||
|
||||
// Non-spec, convenience method.
|
||||
GC::Ref<Promise> create_rejected_promise_from_exception(JS::Realm&, Exception);
|
||||
|
|
Loading…
Add table
Reference in a new issue