Code Monkey home page Code Monkey logo

Comments (3)

axel22 avatar axel22 commented on July 4, 2024

In this particular example, you need to have some determinism - nondeterminism cannot be avoided. The FileAlterationListener represents an external event source, which may trigger zero, one or more events. Since the Promise/Future can hold at most a single event, only the first (that is, the quickest) invocation of onFileCreate can complete the promise object p.

If we use success to complete the promise, then if the external event source emits two file creation events, the onFileCreate will be called twice, and one of the success calls will throw an exception.
With our definition of determinism, this is deterministic, but not particularly useful - if we ignore the exception, then the rest of the program that depends on the value in the promise becomes non-deterministic anyway.

If we use trySuccess, one of the calls will similarly not succeed, but an exception will not be thrown. In cases like this, where you cannot avoid determinism, it's sometimes better to just use trySuccess.

from learning-examples.

normenmueller avatar normenmueller commented on July 4, 2024

In this particular example, you need to have some determinism - nondeterminism cannot be avoided. The FileAlterationListener represents an external event source, which may trigger zero, one or more events. Since the Promise/Future can hold at most a single event, only the first (that is, the quickest) invocation of onFileCreate can complete the promise object p.

Good explanation.

If we use success to complete the promise, then if the external event source emits two file creation events, the onFileCreate will be called twice, and one of the success calls will throw an exception. With our definition of determinism, this is deterministic, but not particularly useful - if we ignore the exception, then the rest of the program that depends on the value in the promise becomes non-deterministic anyway.

If we use trySuccess, one of the calls will similarly not succeed, but an exception will not be thrown. In cases like this, where you cannot avoid determinism, it's sometimes better to just use trySuccess.

"where you cannot avoid nondeterminism", nor?

from learning-examples.

axel22 avatar axel22 commented on July 4, 2024

Yes, typo - "where you cannot avoid nondeterminism".

from learning-examples.

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.