flutter一次调用多个接口 执行多个操作 Future.wait的用法
发布日期:2021-05-14 17:31:55 浏览次数:16 分类:精选文章

本文共 2029 字,大约阅读时间需要 6 分钟。

��������� JavaScript ��������������������������������������������������������������������������������������������� Promise.all ������������������������ Flutter ������������������������������������ Future.wait���

��������������������������� Promise.all ������������

async functiondemo1() {
return true;
}
async functiondemo2() {
return true;
}
async functiondemo3() {
return false;
}
// ���������������
const [, , result] = await Promise.all([demo1(), demo2(), demo3()]);
// ���������
const result = await Promise.all([demo1(), demo2(), demo3()]);
//UI ������������
Demo1:
{result[0]}
Demo2:
{result[1]}
Demo3:
{result[2]}
// ���������������
(demo1, demo2, demo3).then((e) => {
console.log('���������', e); // [true, true, false]
}).catchError((e) => {
console.error('���������', e);
});

��������������� Flutter ��������������� Future.wait ������������������������

Future
syncFunc1() async {
await Future.delayed(const Duration(seconds: 1), () => true);
return true;
}
Future
syncFunc2() async {
await Future.delayed(const Duration(seconds: 2), () => true);
return true;
}
Future
asyncFunc3() async {
await Future.delayed(const Duration(seconds: 3), () => false);
return false;
}
// ���������������
Future
future = Future.wait([syncFunc1(), asyncFunc2(), asyncFunc3()]);
// UI���������
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:

Function Results

Sync Function 1:
{await futureWhenComplete} Sync Function 2:
{await futureWhenComplete} Async Function 3:
{await futureWhenComplete}
).then((e) => {
print('Results: $e');
}).catchError((e) => {
print('Error: $e');
});

������������������������������������������ JavaScript ������ Flutter ��������������������� Promise.all ��� Future.wait ������������������������������������������������������������������������������������������������������������������������

上一篇:flutter try catch和使用loading时的注意事项
下一篇:flutter节流 dart事件节流 搜索节流 连续触发节流的优化写法

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月28日 00时32分04秒