private static bool MoveNextSafe(EcmaIteratorEnumerator iterator) { try { return(iterator.MoveNext()); } catch { // do not explicitly close iterator when abrupt completion from getting next value // mark iterator done when exception throw from iterator.MoveNext() iterator.Done(); throw; } }
public static StatefulIterator GetAsyncIterator(this RuntimeObject obj) { Guard.ArgumentNotNull(obj, "obj"); RuntimeObject method = obj.GetMethod(Symbol.AsyncIterator); if (method == null) { EcmaIteratorEnumerator syncIterator = GetIterator(obj); return(new AsyncFromSyncIterator(syncIterator)); } EcmaValue iterator = method.Call(obj); Guard.ArgumentIsObject(iterator); return(iterator.GetUnderlyingObject <AsyncGenerator>()); }
public static EcmaValue Race([This] EcmaValue thisValue, EcmaValue iterable) { Guard.ArgumentIsObject(thisValue); PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject()); try { using (EcmaIteratorEnumerator iterator = iterable.ForOf()) { EcmaValue resolve = thisValue[WellKnownProperty.Resolve]; while (MoveNextSafe(iterator)) { EcmaValue thenable = resolve.Call(thisValue, iterator.Current); thenable.Invoke(WellKnownProperty.Then, capability.ResolveCallback, capability.RejectCallback); } } } catch (Exception ex) { capability.Reject(EcmaValueUtility.GetValueFromException(ex)); } return(capability.Promise); }
public static EcmaValue All([This] EcmaValue thisValue, EcmaValue iterable) { Guard.ArgumentIsObject(thisValue); PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject()); PromiseAggregator aggregator = new AllFulfilledAggregator(capability); try { using (EcmaIteratorEnumerator iterator = iterable.ForOf()) { EcmaValue resolve = thisValue[WellKnownProperty.Resolve]; while (MoveNextSafe(iterator)) { EcmaValue thenable = resolve.Call(thisValue, iterator.Current); thenable.Invoke(WellKnownProperty.Then, (PromiseResolver)aggregator.CreateHandler().ResolveHandler, capability.RejectCallback); } } aggregator.ResolveIfConditionMet(); } catch (Exception ex) { capability.Reject(EcmaValueUtility.GetValueFromException(ex)); } return(capability.Promise); }