protected static InterceptableEnumerator WrapEnumerator(IEnumerator routine, IPromise promise) { InterceptableEnumerator enumerator = routine is InterceptableEnumerator ? (InterceptableEnumerator)routine : new InterceptableEnumerator(routine); if (promise != null) { enumerator.RegisterConditionBlock(() => !(promise.IsCancellationRequested)); enumerator.RegisterCatchBlock(e => { if (promise != null) { promise.SetException(e); } if (log.IsErrorEnabled) { log.Error(e); } }); enumerator.RegisterFinallyBlock(() => { if (promise != null && !promise.IsDone) { if (promise.GetType().IsSubclassOfGenericTypeDefinition(typeof(IPromise <>))) { promise.SetException(new Exception("No value given the Result")); } else { promise.SetResult(); } } }); } return(enumerator); }