Skip to content

Instantly share code, notes, and snippets.

@chalin
Forked from Sfshaza/main.dart
Last active August 14, 2018 00:10
Show Gist options
  • Save chalin/5e7eb16cd725f0a51926b85e3caa64b5 to your computer and use it in GitHub Desktop.
Save chalin/5e7eb16cd725f0a51926b85e3caa64b5 to your computer and use it in GitHub Desktop.
futures/futures-api
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
printDailyNewsDigest() {
final future = gatherNewsReports();
future.then((news) => print(news));
}
main() {
printDailyNewsDigest();
printWinningLotteryNumbers();
printWeatherForecast();
printBaseballScore();
}
printWinningLotteryNumbers() {
print('Winning lotto numbers: [23, 63, 87, 26, 2]');
}
printWeatherForecast() {
print("Tomorrow's forecast: 70F, sunny.");
}
printBaseballScore() {
print('Baseball score: Red Sox 10, Yankees 0');
}
const news = '<gathered news goes here>';
const oneSecond = Duration(seconds: 1);
final newsStream = new Stream.periodic(oneSecond, (_) => news);
// Imagine that this function is more complex and slow. :)
Future gatherNewsReports() => newsStream.first;
// Alternatively, you can get news from a server using features
// from either dart:io or dart:html. For example:
//
// import 'dart:html';
//
// Future gatherNewsReportsFromServer() => HttpRequest.getString(
// 'https://www.dartlang.org/f/dailyNewsDigest.txt',
// );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment