Promise equivalent in C#

In C#: Task<T> is a future (or Task for a unit-returning future). TaskCompletionSource<T> is a promise. So your code would translate as such: // var promise = new Promise<MyResult>; var promise = new TaskCompletionSource<MyResult>(); // handlerMyEventsWithHandler(msg => promise.Complete(msg);); handlerMyEventsWithHandler(msg => promise.TrySetResult(msg)); // var myResult = promise.Future.Await(2000); var completed = await Task.WhenAny(promise.Task, Task.Delay(2000)); if (completed == … Read more

Pass multiple parameters to concurrent.futures.Executor.map?

One argument that is repeated, one argument in c from itertools import repeat for result in executor.map(f, repeat(a), c): pass Need to unpack items of c, and can unpack c from itertools import izip for result in executor.map(f, *izip(*c)): pass Need to unpack items of c, can’t unpack c Change f to take a single … Read more

Dartlang wait more than one future

You can use Future.wait to wait for a list of futures: import ‘dart:async’; Future main() async { var data = []; var futures = <Future>[]; for (var d in data) { futures.add(d.loadData()); } await Future.wait(futures); } DartPad example

Get the status of a std::future

You are correct, and apart from calling wait_until with a time in the past (which is equivalent) there is no better way. You could always write a little wrapper if you want a more convenient syntax: template<typename R> bool is_ready(std::future<R> const& f) { return f.wait_for(std::chrono::seconds(0)) == std::future_status::ready; } N.B. if the function is deferred this … Read more

Transform Java Future into a CompletableFuture

If the library you want to use also offers a callback style method in addition to the Future style, you can provide it a handler that completes the CompletableFuture without any extra thread blocking. Like so: AsynchronousFileChannel open = AsynchronousFileChannel.open(Paths.get(“/some/file”)); // … CompletableFuture<ByteBuffer> completableFuture = new CompletableFuture<ByteBuffer>(); open.read(buffer, position, null, new CompletionHandler<Integer, Void>() { @Override … Read more

Futures vs. Promises

Future and Promise are the two separate sides of an asynchronous operation. std::promise is used by the “producer/writer” of the asynchronous operation. std::future is used by the “consumer/reader” of the asynchronous operation. The reason it is separated into these two separate “interfaces” is to hide the “write/set” functionality from the “consumer/reader”. auto promise = std::promise<std::string>(); … Read more

Waiting on a list of Future

You can use a CompletionService to receive the futures as soon as they are ready and if one of them throws an exception cancel the processing. Something like this: Executor executor = Executors.newFixedThreadPool(4); CompletionService<SomeResult> completionService = new ExecutorCompletionService<SomeResult>(executor); //4 tasks for(int i = 0; i < 4; i++) { completionService.submit(new Callable<SomeResult>() { public SomeResult call() … Read more

What are the differences between Deferred, Promise and Future in JavaScript?

These answers, including the selected answer, are good for introducing promises conceptually, but lacking in specifics of what exactly the differences are in the terminology that arises when using libraries implementing them (and there are important differences). Since it is still an evolving spec, the answer currently comes from attempting to survey both references (like … Read more