Giter Club home page Giter Club logo

Comments (7)

clue avatar clue commented on May 18, 2024

Some pseudo code (only for the reference):

    $deferred = new Deferred(function ($resolve, $reject) use (&$timer, $loop, $promise) {
        // try to cancel() the given promise (only available for *some* promises)
        // this will *usually* reject the promise, but there's no guarantee
        if ($promise instanceof CancellablePromise) {
            $promise->cancel();
        }

        // explicitly stop the timer and reject the resulting promise
        // canceling the inner promise may already have cancelled the resulting promise
        $loop->cancelTimer($timer);
        $reject(new \RuntimeException('Promise cancelled'));
    });

from promise-timer.

clue avatar clue commented on May 18, 2024

Cancellation support requires the Promise v2.2 API. Other versions should be supported in a way that cancellation is not available (has no effect).

Cancellation support for resolve() and reject() is straight forward.

from promise-timer.

clue avatar clue commented on May 18, 2024

Cancellation support for timeout() is slightly more complicated.

I think we agree on this:

  • Cancelling the input promise should be possible (irrespective of the timeout handling)
  • Once the timer fires, the input promise should be cancelled and the result rejected (as discussed in #9, also the current behavior)

Cancelling the resulting timeout promise should also be possible (this has no effect as of yet).
The effect on the resulting timeout promise is pretty easy: abort all timers and reject with an Exception describing the cancellation.

However, it's a bit unclear which effect this should have on the input promise.

Two options:

  • Only cancel the resulting timeout promise but leave the input promise alone
  • Cancelling the timeout promise should also cancel the input promise

I guess both might have valid uses?

from promise-timer.

clue avatar clue commented on May 18, 2024

Example code:

$input = fetchFromRemoteApi();
$timeout = Timer\timeout($input, 5.0, $loop);

$timeout->cancel();

The $timeout promise should be cancelled and rejected.

Should the $input promise be cancelled and rejected?

Two possible interpretations:

  • We cancelled only the timeout timer: $input should be left alone
  • We cancelled the whole operation: $input should also be cancelled (and possibly rejected)

from promise-timer.

clue avatar clue commented on May 18, 2024

Let's consider some use cases where the timeout() function will be used in the real world.

Here's a gist of how a TimeoutConnector could possibly work: https://gist.github.com/clue/75d00d9c2ca2e931661c#file-timeoutconnector-php-L20

In cases like these, it's probably safe to assume that if you cancel the resulting $timeout promise, the $input promise should also be cancelled.

As such, I'm voting for making this the default.

I'm still curious if we come up with some use cases where the other behavior makes sense.

from promise-timer.

jsor avatar jsor commented on May 18, 2024

I also had the idea that $timeout->cancel() means, cancel the timeout ( == remove the timer). For the example gist that would mean, connect without a timeout. But i think that this really doesn't make sense. I'd consider the cancel() call as the usual "i'm not longer interested in the result", ergo cancel also the input promise.

from promise-timer.

clue avatar clue commented on May 18, 2024

Thanks for the feedback @jsor!

I think we agree here. I guess the confusion can be cleared up by providing some documentation (see #14).

I've just filed PR #13 as discussed above, care to review?

I'd like to make this ticket actionable. IMO we should focus on the main use case here and close the ticket once the PR lands. The other use case can be addressed in a separate ticket once deemed useful.

from promise-timer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.