如何使用循环为 `Future.wait()` 提供一个 Futures 列表

发布于 2025-01-15 11:00:50 字数 906 浏览 2 评论 0原文

我正在使用 Future.wait() 来获取一些数据,如下面的代码所示:
我调用函数 Future.delayed() 在每次调用之间等待 1 秒,因为我将限制设置为每秒 1 个请求

    await Future.wait([

        Future.delayed(const Duration(milliseconds: 1000))
            .then((value) => getPartNumber(1)
            .then((value) => addPartToList((value.data)))),

        Future.delayed(const Duration(milliseconds: 2000))
            .then((value) => getPartNumber(2)
            .then((value) => addPartToList((value.data)))),

        Future.delayed(const Duration(milliseconds: 3000))
            .then((value) => getPartNumber(3)
            .then((value) => addPartToList((value.data)))),

        Future.delayed(const Duration(milliseconds: 4000))
            .then((value) => getPartNumber(4)
            .then((value) => addPartToList((value.data)))),

    ]);

所以我想要做的是使用循环而不是手动重复调用4次

I'm using Future.wait() to get some data like in the code below:
I call the function Future.delayed() to wait 1 second between every call because I have a limit set to 1 request per second

    await Future.wait([

        Future.delayed(const Duration(milliseconds: 1000))
            .then((value) => getPartNumber(1)
            .then((value) => addPartToList((value.data)))),

        Future.delayed(const Duration(milliseconds: 2000))
            .then((value) => getPartNumber(2)
            .then((value) => addPartToList((value.data)))),

        Future.delayed(const Duration(milliseconds: 3000))
            .then((value) => getPartNumber(3)
            .then((value) => addPartToList((value.data)))),

        Future.delayed(const Duration(milliseconds: 4000))
            .then((value) => getPartNumber(4)
            .then((value) => addPartToList((value.data)))),

    ]);

So what I wanna do is to use a loop instead of repeating the call manually 4 times

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2025-01-22 11:00:50

为什么不能只使用 for 循环并在其间等待 1 秒以不超过速率限制。

for(int i = 1; i <= 4; i++){
  await Future.delayed(const Duration(seconds: 1));
  await getPartNumber(i).then((value) => addPartToList(value.data));
}

这也确保了每秒不会有超过一个请求进入,因为中间的时间是等待的。
或者,如果您希望以与代码中相同的方式处理等待时间,则以下方法也适用:

await for(var i in Stream.periodic(const Duration(seconds: 1), (i)=>i).take(5)){
  if(i == 0) continue;
  await getPartNumber(i).then((value) => addPartToList(value.data));
}

或者,如果您确实想使用 Future.wait() 来执行此操作(我个人不喜欢),请执行以下操作它:

await Future.wait(
  Iterable.generate(4, (i)=>Future.delayed(const Duration(seconds: i + 1))
    .then((value) => getPartNumber(i + 1))
    .then((value) => addPartToList(value.data))
);

await Future.wait([
  for(int i = 1; i <= 4; i++)
    Future.delayed(const Duration(seconds: i))
      .then((value) => getPartNumber(i)
      .then((value) => addPartToList(value.data))
]);

Why can't you just use a for loop and wait 1 second in between to not exceed the rate limit.

for(int i = 1; i <= 4; i++){
  await Future.delayed(const Duration(seconds: 1));
  await getPartNumber(i).then((value) => addPartToList(value.data));
}

This also ensures that no more than one request per second comes in, as the time in between is waited.
Or if you want the waiting time to be handled the same way as in your code, the following also works:

await for(var i in Stream.periodic(const Duration(seconds: 1), (i)=>i).take(5)){
  if(i == 0) continue;
  await getPartNumber(i).then((value) => addPartToList(value.data));
}

Or if you really want to do it with Future.wait(), which I personally don't like, here's how to do it:

await Future.wait(
  Iterable.generate(4, (i)=>Future.delayed(const Duration(seconds: i + 1))
    .then((value) => getPartNumber(i + 1))
    .then((value) => addPartToList(value.data))
);

or

await Future.wait([
  for(int i = 1; i <= 4; i++)
    Future.delayed(const Duration(seconds: i))
      .then((value) => getPartNumber(i)
      .then((value) => addPartToList(value.data))
]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文