Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Promise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public interface IPromise<PromisedT>
/// </summary>
IPromise<PromisedT> Catch(Func<Exception, PromisedT> onRejected);

IPromise<ConvertedT> Catch<ConvertedT>(Func<Exception, IPromise<ConvertedT>> onRejected);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs the <summary>...</summary> comment on it so that a description of how the method works shows up in IntelliSense.

This should also return IPromise<PromisedT>, not IPromise<ConvertedT>. This is because Catch doesn't always run, so if we try to convert the type of the promise in the catch handler, we have no way of knowing after the catch whether the type is the original PromisedT or the ConvertedT.

promiseReturningAnInt()
    .Then<int>(value => value + 1) 
    .Catch<string>(ex => Promise.Resolved<string>(ex.Message))
    .Then<???>(arg => /* no way to know whether arg should be an int or a string */);

The only case where you can return a different type of promise in the rejected handler is in the overload of .Then where both the resolved and rejected handlers are guarenteed to return the same type - see Promise.cs line 75


/// <summary>
/// Add a resolved callback that chains a value promise (optionally converting to a different value type).
/// </summary>
Expand Down Expand Up @@ -580,6 +582,31 @@ public IPromise<PromisedT> Catch(Func<Exception, PromisedT> onRejected)

return resultPromise;
}

public IPromise<ConvertedT> Catch<ConvertedT>(Func<Exception, IPromise<ConvertedT>> onRejected) {
var resultPromise = new Promise<ConvertedT>();
resultPromise.WithName(Name);

Action<Exception> rejectHandler = ex =>
{
try
{
onRejected(ex)
.Then(
chainedValue => resultPromise.Resolve(chainedValue),
callbackEx => resultPromise.Reject(callbackEx)
);
}
catch (Exception callbackEx)
{
resultPromise.Reject(callbackEx);
}
};

InvokeHandler(rejectHandler, this, rejectionException);

return resultPromise;
}

/// <summary>
/// Add a resolved callback that chains a value promise (optionally converting to a different value type).
Expand Down
20 changes: 20 additions & 0 deletions Promise_NonGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public interface IPromise
/// </summary>
IPromise Catch(Action<Exception> onRejected);

IPromise<ConvertedT> Catch<ConvertedT>(Func<Exception, IPromise<ConvertedT>> onRejected);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


/// <summary>
/// Add a resolved callback that chains a value promise (optionally converting to a different value type).
/// </summary>
Expand Down Expand Up @@ -671,6 +673,24 @@ public IPromise Catch(Action<Exception> onRejected)

return resultPromise;
}

public IPromise<ConvertedT> Catch<ConvertedT>(Func<Exception, IPromise<ConvertedT>> onRejected) {
var resultPromise = new Promise<ConvertedT>();
resultPromise.WithName(Name);

Action<Exception> rejectHandler = ex =>
{
onRejected(ex)
.Then(
chainedValue => resultPromise.Resolve(chainedValue),
callbackEx => resultPromise.Reject(callbackEx)
);
};

InvokeRejectHandler(rejectHandler, this, rejectionException);

return resultPromise;
}

/// <summary>
/// Add a resolved callback that chains a value promise (optionally converting to a different value type).
Expand Down