Exemple #1
0
        public static EcmaValue Reject([This] EcmaValue thisValue, EcmaValue reason)
        {
            Guard.ArgumentIsObject(thisValue);
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject());

            capability.Reject(reason);
            return(capability.Promise);
        }
Exemple #2
0
        public static EcmaValue Resolve([This] EcmaValue thisValue, EcmaValue value)
        {
            Guard.ArgumentIsObject(thisValue);
            if (thisValue.Equals(value[WellKnownProperty.Constructor], EcmaValueComparison.SameValue))
            {
                return(value);
            }
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject());

            capability.Resolve(value);
            return(capability.Promise);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        public static EcmaValue Then([This] EcmaValue thisValue, EcmaValue onfulfill, EcmaValue onreject)
        {
            Promise         promise     = thisValue.GetUnderlyingObject <Promise>();
            RuntimeObject   constructor = RuntimeObject.GetSpeciesConstructor(promise, WellKnownObject.PromiseConstructor);
            PromiseCallback c1          = null;
            PromiseCallback c2          = null;

            if (onfulfill.IsCallable)
            {
                c1 = v => onfulfill.Call(EcmaValue.Undefined, v);
            }
            if (onreject.IsCallable)
            {
                c2 = v => onreject.Call(EcmaValue.Undefined, v);
            }
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(constructor);

            capability.HandlePromise(promise, c1, c2);
            return(capability.Promise);
        }
Exemple #5
0
        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);
        }