Dart 库瀑布原理,如来自 caolan 的异步

标签 dart dart-async

早些时候,我是一名 javascript 程序员,我使用来自 caolan 的库 async 来防止回调 hell async library .

在这些天里,我转向了 dart,异步风格与 javascript 中的风格非常相似。我真的很喜欢 async.waterfall 函数,我可以按顺序执行带有回调的函数。

我怎么能在 Dart 中做到这一点?他们有类似的 dart 库可用吗?

最佳答案

您可以简单地将 Futures 的结果链接在一起:

import 'dart:async';

void main() {
 first()
  .then(second)
  .then(third)
  .then(fourth);
}

Future<String> first() {
  return new Future.sync(() => "first");
}

Future<int> second(String arg ) {
  return new Future.sync(() => 2);
}

Future<List<int>> third(int arg) {
  return new Future.sync(() => [1, 2, 3]);
}

Future<String> fourth(List<int> arg ) {
  return new Future.sync(() => "Final result is: $arg");
}

关于Dart 库瀑布原理,如来自 caolan 的异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24492983/

相关文章:

dart - 如何转换大数以缩短 Dart 中的 K/M/B

flutter - 如何避免每次导航到页面时都重新加载数据

firebase - 检索 Firestore 对象 flutter

android - 在 flutter 中访问外部摄像头时出错 - "The user has not given permission to access the device"

Dart - 流事件超时的单元测试

dart - 从回调内部取消 StreamSubscription

Dart : how can assign the result of httpRequest on a property object?

unit-testing - 如何在Dart中对大量异步流程进行单元测试?

firefox - 删除 Dart 文件的链接会杀死应用程序

firebase - 我们如何在 Firebase/firestore 上运行多个查询来获取 flutter 中的过滤数据?