promises icon indicating copy to clipboard operation
promises copied to clipboard

Promise._catch() causes to lost previous result

Open SergeyA opened this issue 6 years ago • 3 comments

Promise._catch() causes to lost previous result so the next handler in the chain gets null. See: https://github.com/onehilltech/promises/blob/e5b76c56d372acfb200e97b37ecde0e5146f54ee/promises-jvm/src/main/java/com/onehilltech/promises/Promise.java#L520

  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 avatar May 03 '18 19:05 SergeyA

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

hilljh82 avatar May 11 '18 01:05 hilljh82

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 avatar Dec 20 '18 10:12 Kinchkun

@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?

hilljh82 avatar Dec 22 '18 19:12 hilljh82