Dart-异步操作实例

2019-07-07  本文已影响0人  mihope
import 'dart:async';

Future<void> printDailyNewsDigest() async {
  var newsDigest = await gatherNewsReports();
  print(newsDigest);
}

Future printStackInfo() {
  return new Future.delayed(Duration(seconds: 1)).then((_) {
    return '热门股票: 中国人瘦';
  });
}

printWinningLotteryNumbers() {
  print('双色球: [23, 63, 87, 26, 2]');
}

printWeatherForecast() {
  print("天气预报: 23°c, 晴");
}

printBaseballScore() {
  print('球讯: 火箭-湖人');
}

var news = '新闻播报到次结束';
const oneSecond = Duration(seconds: 3);

Future<String> gatherNewsReports() => Future.delayed(oneSecond, () => news);

main() {
  printStackInfo().then((info) {
    print(info);
  });
  printDailyNewsDigest();
  printWinningLotteryNumbers();
  printWeatherForecast();
  printBaseballScore();
}

执行结果:

双色球: [23, 63, 87, 26, 2]
天气预报: 23°c, 晴
球讯: 火箭-湖人
热门股票: 中国人瘦
新闻播报到次结束
上一篇下一篇

猜你喜欢

热点阅读