Skip to content

Cancellable Promise in a Chain #64

@alextosatto

Description

@alextosatto

Hi guys, I have been using this library for a while and I have to say it's very easy to work with it.

I have a concern about cancellable promises, specifically the behaviour of a chain of promises when a promise in the middle has been cancelled.
Let's say we have:

A.then(B).then(C).then(D).then(E)
    .then { _ in
        print("success")
    }.catch { _ in
        print("failed")
    }.cancelled { in
        print("cancelled")
    }

If, for example, C gets cancelled while A and B resolve correctly, I'm expecting that cancelled is printed out, but what I observe is that the sequence gets stuck in pending state.
This behaviour can be checked with the following test that recalls the ones provided with the library, which fails for timeout:

  func test_cancelledPromiseChainable2() {
    let exp = expectation(description: "test_cancelledPromiseChainable2")
    
    let invalidator: InvalidationToken = InvalidationToken()
    invalidator.isCancelled = true
    
    intPromise().then { _ in
        self.test_invalidationToken(token: invalidator)
      }.then { total in
        print("Operation finished with: \(total)")
        XCTFail()
      }.catch { _ in
        print("Operation failed")
      }.cancelled {
        print("Operation cancelled")
        exp.fulfill()
    }
    
    waitForExpectations(timeout: expTimeout, handler: nil)
  }

If I'm not misinterpreting the situation, I will open a PR with a suggested fix, otherwise please let me know the reasoning behind this behaviour.

Thanks,
AT

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions