1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-28 16:42:12 +00:00

LibJS: Implement Promise.any on the Promise constructor

This commit is contained in:
Timothy Flynn 2021-08-21 16:43:38 -04:00 committed by Linus Groh
parent 98d8a858cd
commit 4dffa40a8d
4 changed files with 243 additions and 2 deletions

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/AggregateError.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PromiseReaction.h>
@ -72,4 +73,33 @@ Value PromiseAllResolveElementFunction::resolve_element()
return js_undefined();
}
PromiseAnyRejectElementFunction* PromiseAnyRejectElementFunction::create(GlobalObject& global_object, size_t index, PromiseValueList& errors, PromiseCapability capability, RemainingElements& remaining_elements)
{
return global_object.heap().allocate<PromiseAnyRejectElementFunction>(global_object, index, errors, capability, remaining_elements, *global_object.function_prototype());
}
PromiseAnyRejectElementFunction::PromiseAnyRejectElementFunction(size_t index, PromiseValueList& errors, PromiseCapability capability, RemainingElements& remaining_elements, Object& prototype)
: PromiseResolvingElementFunction(index, errors, move(capability), remaining_elements, prototype)
{
}
Value PromiseAnyRejectElementFunction::resolve_element()
{
auto& vm = this->vm();
auto& global_object = this->global_object();
m_values.values[m_index] = vm.argument(0);
if (--m_remaining_elements.value == 0) {
auto errors_array = Array::create_from(global_object, m_values.values);
auto* error = AggregateError::create(global_object);
error->define_property_or_throw(vm.names.errors, { .value = errors_array, .writable = true, .enumerable = false, .configurable = true });
return vm.call(*m_capability.reject, js_undefined(), error);
}
return js_undefined();
}
}