onehilltech/promises

Promise._catch() causes to lost previous result

Opened this issue · 3 comments

Promise._catch() causes to lost previous result so the next handler in the chain gets null. See:

  public <U> Promise <U> _catch (OnRejected onRejected)
  {
    if (onRejected == null)
      throw new IllegalStateException ("onRejected cannot be null");

    return this.then (null, onRejected);
  }

Maybe it should be replaced by:

  public <U> Promise <U> _catch (OnRejected onRejected)
  {
    if (onRejected == null)
      throw new IllegalStateException ("onRejected cannot be null");

    return this.then (v -> Promise.resolve(v), onRejected);
  }

@SergeyA Do you have an example that illustrates this problem?

Hi,

I have the same issue. Maybe I misunderstand the API?

Example:

Promise.resolve("String")
                ._catch(rejected(error -> {throw new RuntimeException("Does not occur");}))
                .then(resolved(System.out::println));

I create a resolved promise. So the _catch is not executed. However, the resolved value in the then is null.

The solution of SergeyA fixed the problem for me.

@Kinchkun I am pretty sure that if this is a problem, then it was an oversight on my part and the proposed fix will address the problem. Could you create a test case for this scenario, and submit a pull request?