【问题标题】:DART: How to write timeconsuming function returning a FutureDART:如何编写返回 Future 的耗时函数
【发布时间】:2014-01-01 12:50:08
【问题描述】:

我必须编写一个耗时的函数,如果它准备好了,它将返回一个 Future。 下面的方法是否正确,或者我在第 9 行中的耗时算法是否会阻止程序直到它准备好。在这种情况下,我该怎么做才能将控制权交还给事件循环,或者还有什么可以解决的?

Future<int> timeconsumingFunctionReturningFuture(int i) {
  var completer = new Completer();

  if (i==0) { 
    completer.completeError(88);
    return completer.future;
  } else {
    int rc;
    // Line9: rc = timeconsuming algorithm, to calculate rc
    completer.complete(rc);
    return completer.future;
  }
}

Tnx 米查

【问题讨论】:

    标签: dart dart-async


    【解决方案1】:

    您的代码可能无法按预期工作,因为您的算法可能会阻止完成者的返回。 试试这个方法:

    Future<int> timeconsumingFunctionReturningFuture(int i) {
      var completer = new Completer();
    
      if (i==0) { 
        completer.completeError(88);
      } else {
        Timer.run(() {
          int rc;
          // Line9: rc = timeconsuming algorithm, to calculate rc
          completer.complete(rc);
        });
      }
      return completer.future;
    }
    

    这样你的耗时算法会异步运行并立即返回未来。

    我自己没有尝试过,但这个较短的版本也应该可以工作(无需创建完成者)

    return new Future.delayed(Duration.ZERO, () {
        // timeconsuming algorithm
      });
    

    【讨论】:

    • 注意:new Future.delayed(Duration.ZERO, fun)new Future(fun) 相同。
    【解决方案2】:

    Günter Zöchbauer 的解决方案是正确的,但您可以简化代码:

    Future<int> timeconsumingFunctionReturningFuture(int i) {
      if (i == 0) return new Future.error(88);
      return new Future(() {
        int rc;
        // Line9: rc = timeconsuming algorithm, to calculate rc
        return rc;
      });
    }
    

    或者,您甚至可以将错误检查置于 Future 中:

    Future<int> timeconsumingFunctionReturningFuture(int i) {
      return new Future(() {
        if (i == 0) throw 88;
        int rc;
        // Line9: rc = timeconsuming algorithm, to calculate rc
        return rc;
      });
    }
    

    另请注意:如果为i 传递0 是错误(程序员错误),那么同步抛出通常是可以的。然后它的行为方式与在检查模式下传递字符串的方式相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2019-06-26
      • 1970-01-01
      • 2017-07-11
      相关资源
      最近更新 更多