/// <summary> /// Returns a promise that resolves with all of the specified promises have resolved. /// Returns a promise of a tuple of the resolved results. /// </summary> public static IPromise <Tuple <T1, T2> > All <T1, T2>(IPromise <T1> p1, IPromise <T2> p2) { var val1 = default(T1); var val2 = default(T2); var numUnresolved = 2; var promise = new Promise <Tuple <T1, T2> >(); p1 .Catch(e => promise.Reject(e)) .Done(val => { val1 = val; numUnresolved--; if (numUnresolved <= 0) { promise.Resolve(Tuple.Create(val1, val2)); } }); p2 .Catch(e => promise.Reject(e)) .Done(val => { val2 = val; numUnresolved--; if (numUnresolved <= 0) { promise.Resolve(Tuple.Create(val1, val2)); } }); return(promise); }
public IPromise <T> Chain <T>(Func <IPromise <T> > callback) { IPromise <T> promise = new Promise <T>(); Action resolveHandler = delegate { IPromise <T> chainedPromise = callback(); chainedPromise.Catch(promise.Fail).Then(promise.Resolve); promise.CancelRequested += delegate(object sender, PromiseCancelRequestedEventArgs e) { if (chainedPromise.State == PromiseState.Pending) { chainedPromise.RequestCancel(); } }; }; AddResolveHandler(resolveHandler); AddFailHandler <Exception>(promise.Fail); promise.CancelRequested += delegate(object sender, PromiseCancelRequestedEventArgs e) { if (State == PromiseState.Pending) { RequestCancel(); } }; return(promise); }
/// <summary> /// Returns promise that is resolved when jsSearchString evaluates to a non-null value or rejected if it evaluates to null after n iterations. /// </summary> /// <returns>The until ready prom.</returns> /// <param name="jsSearchString">Js search string.</param> /// <param name="iterations">Iterations.</param> /// <param name="iterInterval">Iter interval.</param> private IPromise <JSONNode> WaitUntilReadyProm(string jsSearchString = "", int iterations = 10, float iterInterval = 0.25f) { Debug.Log("Looking for js: " + jsSearchString); Promise <JSONNode> returnProm = new Promise <JSONNode>(); IPromise waitProm = Promise.Resolved(); for (int i = 0; i < iterations; i++) { int iter = i; waitProm = waitProm.Then(() => { //Debug.Log(string.Format("Going through iter {0} in search {1}", iter, jsSearchString)); if (returnProm.CurState != PromiseState.Pending) { //Debug.Log("Return empty promise from 1"); return(Promise.Resolved()); } else { return(browser.EvalJS(jsSearchString) .Then((res) => { //Debug.LogFormat("Checking js {0} with result {1} in iter {2}", jsSearchString, res.Value, iter); if (!res.IsNull) { Debug.Log(string.Format("{0} is Found after {1} iterations", jsSearchString, iter)); returnProm.Resolve(res); } else if (iter == iterations - 1) { Debug.Log(string.Format("NOT Found after {0} iterations", iter)); returnProm.Resolve(null); } //Debug.Log("Return empty promise from 2"); return promTimer.WaitFor(iterInterval); }) .Catch((exc) => { Debug.LogWarningFormat("Sequence exception: {0}", exc.Message); returnProm.Reject(new System.Exception("Sequence exprerienced exception: " + exc.Message)); })); } }); } waitProm.Catch((exc) => Debug.LogWarningFormat("Caught exception in waiting promise: {1}", exc.Message)); //Debug.Log("Returning prom"); return(returnProm); }
public IPromise Sequence(IEnumerable <Func <IPromise> > promises) { List <IPromise> actingPromises = new List <IPromise>(); int promiseCount = promises.Count(); int index = 0; IPromise returnPromise = this; IPromise firstPromise = promises.First()?.Invoke(); actingPromises.Add(firstPromise); Action resolveCallback = null; resolveCallback = delegate { index++; if (index < promiseCount) { IPromise currentPromise = promises.ElementAt(index)?.Invoke(); actingPromises.Add(currentPromise); currentPromise.Then(resolveCallback); currentPromise.Catch(delegate(Exception e) { if (returnPromise.State == PromiseState.Pending) { returnPromise.Fail(e); } }); } else { returnPromise.Resolve(); } }; firstPromise.Then(resolveCallback); firstPromise.Catch(delegate(Exception e) { if (returnPromise.State == PromiseState.Pending) { returnPromise.Fail(e); } }); if (promiseCount == 0) { returnPromise.Resolve(); } // Add request cancellation returnPromise.CancelRequested += delegate(object sender, PromiseCancelRequestedEventArgs e) { IPromise currentPromise = actingPromises.ElementAt(index); if (currentPromise.State == PromiseState.Pending) { currentPromise.RequestCancel(); } }; return(returnPromise); }
public IPromise Catch(Action <Exception> action) { return(_promise.Catch(action)); }