public IAsyncOperation <Exception> Otherwise() { lock (this) { if (_otherwiseClause == null) { _otherwiseClause = new Deferred <Exception>(); if (State == AsyncOperationState.Failed) { _otherwiseClause.Accept(Result !.Error !); } } } return(_otherwiseClause.Operation); }
/// <summary> /// Creates a new GRaff.IAsyncOperation that will resolve when all the specified GRaff.IAsyncOperation objects have resolved. /// </summary> /// <param name="operations">An array of GRaff.IAsyncOperation objects.</param> /// <returns>A GRaff.IAsyncOperation that will resolve when all the specified GRaff.IAsyncOperation objects are resolved.</returns> /// <remarks> /// If all operations are accepted, the aggregated operation will be accepted. If any of the specified operations are rejected, /// the aggregated operation will be rejected by an AggregateException, containing an array of System.Exception objects, ///</remarks> public static IAsyncOperation All(params IAsyncOperation[] operations) { if (operations == null || operations.Length == 0) { return(Async.Operation()); } var deferred = new Deferred(); var exceptions = new Exception[operations.Length]; var remainingOperations = operations.Length; var errorsOccurred = false; for (int i = 0; i < operations.Length; i++) { int index = i; operations[i].Catch <Exception>(ex => { exceptions[index] = ex; errorsOccurred = true; }).ThenWait(() => { if (Interlocked.Decrement(ref remainingOperations) == 0) { if (errorsOccurred) { exceptions = exceptions.Select(ex => ex ?? new Exception(null)).ToArray(); deferred.Reject(new AggregateException(exceptions)); } else { deferred.Accept(); } } }); } return(deferred.Operation); }