join<T> static method

等待所有任务完成

Implementation

static Future<List<CompletableFuture<T>>> join<T>(
    List<CompletableFuture<T>>? fs) async {
  Completer<List<CompletableFuture<T>>> completer = Completer();
  if (fs == null || fs.isEmpty) {
    completer.complete([]);
  }
  int count = fs!.length;
  Set<int> ids = {};
  for (var f in fs) {
    void sub() {
      if (!ids.contains(f.hashCode)) {
        ids.add(f.hashCode);
        count--;
        if (count == 0) {
          completer.complete(fs);
        }
      }
    }

    f.whenComplete(sub);
    f.onCancel(sub);
  }
  return completer.future;
}