Code Monkey home page Code Monkey logo

Comments (6)

mishagray avatar mishagray commented on August 21, 2024

I need to document that. But there is a class called FutureBatch.

FutureBatchOf is a typesafe version. But it requires all the futures to be the same type.

FutureBatch is a type alias of FutureBatchOf.

And if has a var "future" which will succeed iff all of its subfutures succeed.

They also execute in parallel. The result is an array [Any]

from futurekit.

mishagray avatar mishagray commented on August 21, 2024

Didn't mean to close this yet.

I'll get the documentation and example up this weekend.

from futurekit.

zdnk avatar zdnk commented on August 21, 2024

Well, I think there should be more options of handling this ... Like complete if any of the futures is completed ... complete if all the futures are completed, etc ..

from futurekit.

mishagray avatar mishagray commented on August 21, 2024

So the current implementation waits for all to finish. Which I use a lot in my code. It also keeps error handling simple and reliable.

There is also an internal future that first returns a an array of Completion<Any> if you want to query the individual futures results. But I never have used that. I mostly just want to fail if any fail. One option I have considered adding is to stop immediately after the first failure. (It currently doesn't do that). Which hasn't been a huge problem for my existing apps so I haven't added that feature. But might be worth adding but it does add complexity.

It wouldn't be hard to build a "return a value immediately if any finish". I can show an example of how to do that. But not sure if it should be part of FutureBatch or just included as an example. (Cause it's dead simple).

For that you can just create a single Promise, and than iterate through an array of Futures calling onComplete or OnSuccess that try to complete the Promise. And use the future from that Promise.
Do you care if the others succeed? Should they be canceled?

I've been intentionally trying to remove complexity or confusion from the initial launch.

from futurekit.

mishagray avatar mishagray commented on August 21, 2024

Added this:
#5

Which changes FutureBatch a bit. Still need to add a full set of test cases. But it should do what you want.

There are now 3 vars
completionFuture - returns an array of Completions<T>. Always returns '.Success' with an array of completion values. Does't finish until all sub-futures do.
batchFuture - returns an array[T]. Will Fail if one sub-future fails. Will return Cancel if no Fails, but one sub-future is canceled. Completes only after all sub-futures have completed.
future - exactly like batchFuture - EXCEPT it will stop immediately if one of the sub-futures returns Fail or Cancel. It doesn't wait for other sub-futures to complete.

Also add
cancelRemainingFuturesOnFirstFailOrCancel() and cancelRemainingFuturesOnFirstFail() which will try to cancel other sub-futures if one of the sub-futures fail. (To help speed up halt/cleanup).

onEachComplete() which lets you define a block that will run as soon as each sub-future completes.

I think this should be flexible enough to cover must needs for dealing with lots of sub-futures.

I killed append (for now)which would let you add a new future to an existing batch (that has already started). Both cause I think it may lead to difficult to manage logic AND it was hard to implement once I added onEachComplete() and'cancelRemainingFuturesOnFirstFailOrCancel(). Might return in a future version of FutureKit.

from futurekit.

mishagray avatar mishagray commented on August 21, 2024

This has been closed in the latest releases.

from futurekit.

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.