what is the difference between thunk, futures, and promises?

An example of each, using javascript since everybody can read it. Please don’t use this code in production, use a real library, there are plenty of good ones. var a, b, c, async_obj; // assume exist // CommonJS – for reference purposes try { async_obj.asyncMethod(a, b, c, function (error1, result1) { if (error1) { console.error(error1); … Read more

Dart: How do you make a Future wait for a Stream?

You can use the Stream method firstWhere to create a future that resolves when your Stream emits a true value. Future<bool> whenTrue(Stream<bool> source) { return source.firstWhere((bool item) => item); } An alternative implementation without the stream method could use the await for syntax on the Stream. Future<bool> whenTrue(Stream<bool> source) async { await for (bool value … Read more

Python’s `concurrent.futures`: Iterate on futures according to order of completion

executor.map(), like the builtin map(), only returns results in the order of the iterable, so unfortunately you can’t use it to determine the order of completion. concurrent.futures.as_completed() is what you’re looking for – here’s an example: import time import concurrent.futures times = [3, 1, 2] def sleeper(secs): time.sleep(secs) print(‘I slept for {} seconds’.format(secs)) return secs … Read more

Map a Future for both Success and Failure

Edit 2017-09-18: As of Scala 2.12, there is a transform method that takes a Try[T] => Try[S]. So you can write val future = … // Future[T] val mapped = future.transform { case Success(_) => Success(“OK”) case Failure(_) => Success(“KO”) } For 2.11.x, the below still applies: AFAIK, you can’t do this directly with a … Read more

Flutter Future vs bool type

You need to get the bool out of Future<bool>. Use can then block or await. with then block _checkConnection() { Utiliy.checkConnection().then((connectionResult) { Utility.showAlert(context, connectionResult ? “OK”: “internet needed”); }) } with await _checkConnection() async { bool connectionResult = await Utiliy.checkConnection(); Utility.showAlert(context, connectionResult ? “OK”: “internet needed”); } For more details, refer here.

Dart Future.wait for multiple futures and get back results of different types

In Dart 3, you should use a Record of Futures instead of a List/Iterable so that you can have heterogeneous types. Dart 3 provides wait extensions for such Records that are similar to Future.wait. (See sudormrfbin’s answer for an example.) If you must use Future.wait, you need to adapt each of your Future<T>s to a … Read more

ListenableFuture to scala Future

The second option is best, it keeps everything asynchronous. but… you can do one better and abstract the solution into a reusable pattern: implicit class RichListenableFuture[T](lf: ListenableFuture[T]) { def asScala: Future[T] = { val p = Promise[T]() Futures.addCallback(lf, new FutureCallback[T] { def onFailure(t: Throwable): Unit = p failure t def onSuccess(result: T): Unit = p … Read more