Skip to content

Commit a204c21

Browse files
committed
Implement Promise Race
1 parent be67f26 commit a204c21

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

nova_vm/src/ecmascript/builtins/control_abstraction_objects/promise_objects/promise_abstract_operations/promise_group_record.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ use crate::{
2323
},
2424
};
2525

26-
#[derive(Debug, Clone, Copy)]
26+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
2727
pub enum PromiseGroupType {
2828
All,
2929
AllSettled,
3030
Any,
31+
Race,
3132
}
3233

3334
#[derive(Debug, Clone, Copy)]
@@ -89,6 +90,14 @@ impl<'a> PromiseGroup<'a> {
8990
self.reject(agent, index, value.unbind(), gc.reborrow());
9091
}
9192
},
93+
PromiseGroupType::Race => match reaction_type {
94+
PromiseReactionType::Fulfill => {
95+
self.immediately_resolve(agent, value.unbind(), gc.reborrow());
96+
}
97+
PromiseReactionType::Reject => {
98+
self.immediately_reject(agent, value.unbind(), gc.nogc());
99+
}
100+
},
92101
}
93102
}
94103

nova_vm/src/ecmascript/builtins/control_abstraction_objects/promise_objects/promise_constructor.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ impl PromiseConstructor {
285285
/// > method.
286286
fn race<'gc>(
287287
agent: &mut Agent,
288-
_this_value: Value,
289-
_arguments: ArgumentsList,
288+
this_value: Value,
289+
arguments: ArgumentsList,
290290
gc: GcScope<'gc, '_>,
291291
) -> JsResult<'gc, Value<'gc>> {
292-
Err(agent.todo("Promise.race", gc.into_nogc()))
292+
promise_group(agent, this_value, arguments, PromiseGroupType::Race, gc)
293293
}
294294

295295
/// ### [27.2.4.6 Promise.reject ( r )](https://tc39.es/ecma262/#sec-promise.reject)
@@ -680,6 +680,7 @@ fn promise_group<'gc>(
680680
/// ### [27.2.4.1.2 PerformPromiseAll ( iteratorRecord, constructor, resultCapability, promiseResolve )](https://tc39.es/ecma262/#sec-performpromiseall)
681681
/// ### [27.2.4.2.1 PerformPromiseAllSettled ( iteratorRecord, constructor, resultCapability, promiseResolve )](https://tc39.es/ecma262/#sec-performpromiseallsettled)
682682
/// ### [27.2.4.3.1 PerformPromiseAny ( iteratorRecord, constructor, resultCapability, promiseResolve )](https://tc39.es/ecma262/#sec-performpromiseany)
683+
/// ### [27.2.4.5.1 PerformPromiseRace ( iteratorRecord, constructor, resultCapability, promiseResolve )](https://tc39.es/ecma262/#sec-performpromiserace)
683684
#[allow(clippy::too_many_arguments)]
684685
fn perform_promise_group<'gc>(
685686
agent: &mut Agent,
@@ -743,6 +744,10 @@ fn perform_promise_group<'gc>(
743744

744745
// b. If next is done, then
745746
let Some(next) = next else {
747+
if promise_group_type == PromiseGroupType::Race {
748+
return Ok(promise.get(agent));
749+
}
750+
746751
*iterator_done = true;
747752
let promise_group = promise_group_reference.get(agent).bind(gc.nogc());
748753
let data = promise_group.get_mut(agent);

0 commit comments

Comments
 (0)