Speed up development with full-stack environments for every branch.

Learn More

async.Future.wait [Dart]

Forked from Basic Dart to JS Example.

212 Runs 1452 Views 245 Copies

https://api.dartlang.org/docs/channels/stable/latest/dart_async/Future.html#wait

Saved

Saved

damondouglas 16

damondouglas
published 3 years ago

import 'dart:async';

void main() {
	Future.wait(
		[
			waitFor(new Duration(seconds: 4), 1),
			waitFor(new Duration(seconds: 1), 2),
			waitFor(new Duration(seconds: 2), 3)
		]
	)
	.then((List<String> result){
		result.forEach((String value){
			print(value);
		});
	});
}

Future<String> waitFor(Duration timeout, int order) {
	var completer = new Completer();

	var timer = new Timer(timeout, (){
		var now = new DateTime.now();
		completer.complete("$order: ${now.toString()}");
	});

	return completer.future;
}
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).