Dart async proc
//dart
import 'dart:io';
import 'dart:async';
Future printDailyNewsDigest1() {
print('A:');
File file = new File("dailyNewsDigest.txt");
Future future = file.readAsString();
return future;
}
Future printDailyNewsDigest2(String content) {
print('A:$content');
File file = new File("dailyNewsDigest.txt");
Future future = file.readAsString();
return future;
}
Future printDailyNewsDigest3(String content) {
print('A:$content');
File file = new File("dailyNewsDigest.txt");
Future future = file.readAsString();
return future;
}
Future printDailyNewsDigest_1() {
print('B:');
File file = new File("dailyNewsDigest.txt");
Future future = file.readAsString();
return future;
}
Future printDailyNewsDigest_2() {
print('B:');
File file = new File("dailyNewsDigest.txt");
Future future = file.readAsString();
return future;
}
Future printDailyNewsDigest_3() {
print('B:');
File file = new File("dailyNewsDigest.txt");
Future future = file.readAsString();
return future;
}
void chooseBestResponse(List ress) {
print(ress.length);
String B1 = ress[0];
String B2 = ress[1];
String B3 = ress[2];
print('B:[1]$B1');
print('B:[2]$B2');
print('B:[3]$B3');
}
void main() {
printDailyNewsDigest1().then(printDailyNewsDigest2)
.then(printDailyNewsDigest3)
.then((content)=>print('A:$content'))
.catchError((e)=>handleError(e));
Future.wait([printDailyNewsDigest_1(), printDailyNewsDigest_2(), printDailyNewsDigest_3()])
.then(chooseBestResponse)
.catchError((e)=>handleError(e));
}
//执行结果
C:\Dart-pro\demo\4>dart future.dart
A:
B:
B:
B:
A:Winning lotto numbers: [23, 63, 87, 26, 2]
Tomorrow's forecast: 70F, sunny.
Baseball score: Red Sox 10, Yankees 0
3
B:[1]Winning lotto numbers: [23, 63, 87, 26, 2]
Tomorrow's forecast: 70F, sunny.
Baseball score: Red Sox 10, Yankees 0
B:[2]Winning lotto numbers: [23, 63, 87, 26, 2]
Tomorrow's forecast: 70F, sunny.
Baseball score: Red Sox 10, Yankees 0
B:[3]Winning lotto numbers: [23, 63, 87, 26, 2]
Tomorrow's forecast: 70F, sunny.
Baseball score: Red Sox 10, Yankees 0
A:Winning lotto numbers: [23, 63, 87, 26, 2]
Tomorrow's forecast: 70F, sunny.
Baseball score: Red Sox 10, Yankees 0
A:Winning lotto numbers: [23, 63, 87, 26, 2]
Tomorrow's forecast: 70F, sunny.
Baseball score: Red Sox 10, Yankees 0
Finally:
异步执行
printDailyNewsDigest1执行后,立即执行Future.wait
printDailyNewsDigest_1(), printDailyNewsDigest_2(), printDailyNewsDigest_3()执行完后,进入回调过程
回调过程首先进入printDailyNewsDigest1的回调,然后进入Future.then,然后进入printDailyNewsDigest2回调,然后进入printDailyNewsDigest3回调。
Dart 的异步处理过程不太好用,要用最好还是一个future一个then回调来用,因为它的回调时间你可能把握不住!
不过,单线程的Dart给了你异步的能力,你还能要求什么呢。
开整吧,祝你好运!
Dart async proc的更多相关文章
- Dart
Dart异步与阻塞 import 'dart:async'; import 'dart:io'; void main() async { for(int i = 0;i<10;i++) { as ...
- Dart - Isolate 并发
在Dart中实现并发可以用Isolate,它是类似于线程(thread)但不共享内存的独立运行的worker,是一个独立的Dart程序执行环境.其实默认环境就是一个main isolate. 在Dar ...
- Dart 基础重点截取 Dart 2 20180417
官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...
- Flutter Dart中的异步
以下内容从官网得到: https://webdev.dartlang.org/articles/performance/event-loop Even-Looper Dart是单线程模型,也就没有了所 ...
- Flutter与Dart 入门
Flutter简介 Flutter是google推出的,一个使用Dart语言开发的跨平台移动UI框架,通过自建绘制引擎,能高性能.高保真地进行Android和IOS开发. Flutter是什么 Flu ...
- Dart异步与消息循环机制
Dart与消息循环机制 翻译自https://www.dartlang.org/articles/event-loop/ 异步任务在Dart中随处可见,例如许多库的方法调用都会返回Future对象来实 ...
- 30分钟掌握Dart语言
在Dart中,一切都是对象,一切对象都是class的实例,哪怕是数字类型.方法甚至null都是对象,所有的对象都是继承自Object 虽然Dart是强类型语言,但变量类型是可选的因为Dart可以自动推 ...
- Dart Socket 与Java Socket连接
------------------------------------------------------------- Dart SocketClient----------------- ...
- Flutter学习笔记(2)--Dart语言简介
Dart简介: Dart诞生于2011年10月10日,Dart是一种"结构化的web编程"语言,Dart虽然是谷歌开发的计算机编程语言,但后来被ECMA认定位标准,这门语言用于We ...
随机推荐
- 记一次 windows server 2012R2 上安装 MSSQL2005 及网站发布
背景: 原因是因为没有购买新的数据库授权.由于之前服务器是windows 2003 由于后绪网站的版本可能会升到.net4.5 据说 2003上安装不起 所有由另一同事到客户安装一个window se ...
- MySQL介绍,下载,安装,配置
MySQL用了很多年了,今天写个总结. 一.介绍 MySQL是开源软件,后来归Oracle所有.开源便于软件的完善改进.但开源不等于滥用,也不等于完全免费.MySQL有商业版,商业用途是付费的.也有免 ...
- 将音乐生成波浪图形,JavaScript Html5
x 省略废话(N+)... Windows Media Palyer中的经典波浪形 自己也行动手做一个,最好是JavaScript实现的, 搜索到了资源部分关键词"HTML5 频谱" ...
- 阿里云不同账号之间相同地域的VPC网络互访
今天实际操作了一下,在这篇随笔中记录一下以备忘,主要参考阿里云帮助文档-不同账号下专有网络内网互通. 实现场景:账号A的VPC网络中的ECS访问账号B的VPC网络中的ECS与RDS(地域都在华东1), ...
- sql join 语句的小总结
CREATE TABLE Persons ( id INT PRIMARY KEY, LastName CHAR() NOT NULL, FirstName VARCHAR (), address V ...
- Arrays 类的一些常见用法
package cn.ljs; import java.util.Arrays; public class ArrayDemo { public static void main(String [] ...
- [No0000172]Android Studio设置HTTP代理(可用)
android SDK下载:http://www.androiddevtools.cn . 禁止第一次启动 到AS安装目录,打开bin目录,编辑idea.properties, 在文件末尾添加: di ...
- Chap2:二进数值与记数系统[Computer Science Illuminated]
1 基数(base):记数系统的基本数值,规定了这个系统中使用的数字量和数位位置的值 2 数字采用位置计数法进行编写 位置计数法(positional notation):一种表达数字的系统,数位按顺 ...
- word 使用总结
1.标题: 开始->标题栏 2.插入目录: 引用---->更新目录 3.保持分页:页面布局->分隔符->分页符
- 《Mysql ALTER基本操作》
一:ALTER 添加单列 - 语法 - ALTER TABLE 表名 ADD 列名 定义类型 [FIRST(列将加入最上方) | AFTER 字段名(列加入某某字段之后) ] - 示例 `user` ...