首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未来序列

未来序列
EN

Stack Overflow用户
提问于 2014-12-14 09:33:18
回答 2查看 672关注 0票数 0

在下面的代码中,我认为f1 > f2 > f3将是调用顺序,但只有f1被调用。如何才能使这3个函数按顺序调用?

我已经在main函数中添加了以下内容,它按照预期的方式工作,但是我想知道是否有其他明确的方法来实现相同的结果?

代码语言:javascript
复制
print('Starting main');
List<Future> futures=new List<Future>();
Future v1=f1();
Future v2=f2();
Future v3=f3();
futures.add(v1);
futures.add(v2);
futures.add(v3);
Future.wait(futures);
print('Leaving main');

import 'dart:async';

Duration d1 = new Duration(seconds: 5);
Duration d2 = new Duration(seconds: 10);
Duration d3 = new Duration(seconds: 15);

bool r1 = false;
bool r2 = false;
bool r3 = false;

void cb1() {
    print('Entering CB1');
    r1 = true;
    print('Leaving CB1');
}

void cb2() {
    print('Entering CB2');
    r2 = true;
    print('Leaving CB2');
}

void cb3() {
    print('Entering CB3');
    r3 = true;
    print('Leaving CB3');
}

Timer t1;
Timer t2;
Timer t3;

Future<bool> start1() {
    print('Entering start1');
    Completer<bool> r = new Completer();
    r.future.then((_) {
        while (!r1) {

        }
        print('Completing start1');
        r.complete(true);
    });

    print('Leaving start1');
    return r.future;
}

Future<bool> start2() {
    print('Entering start2');
    Completer<bool> r = new Completer();
    r.future.then((_) {
        while (!r2) {

        }
        print('Completing start2');
        r.complete(true);
    });

    print('Leaving start2');
    return r.future;
}

Future<bool> start3() {
    print('Entering start3');
    Completer<bool> r = new Completer();
    r.future.then((_) {
        while (!r3) {

        }
        print('Completing start3');
        r.complete(true);
    });

    print('Leaving start3');
    return r.future;
}

Future<bool> f1() {
    print('Entering f1');
    Completer<bool> result = new Completer();
    t1 = new Timer(d1, cb1);
    result.complete(start1());

    print('Leaving f1');
    return result.future;
}

Future<bool> f2() {
    print('Entering f2');
    Completer<bool> result = new Completer();
    t2 = new Timer(d2, cb2);
    result.complete(start2());

    print('Leaving f2');
    return result.future;
}

Future<bool> f3() {
    print('Entering f3');
    Completer<bool> result = new Completer();
    t3 = new Timer(d3, cb3);
    result.complete(start3());

    print('Leaving f3');
    return result.future;
}

void main() {
    print('Starting main');
    f1().then((_) {
        f2().then((_) {
            f3().then((_) {

            });
        });
    });
    print('Leaving main');
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-14 10:13:35

首先,你应该澄清你为什么需要这个。我真的不明白你为什么希望它们被依次执行--你应该给我们更多关于它背后的概念的信息。

请给我们更多关于你想要完成的事情的信息!下面的代码以某种方式实现了我认为您想要的结果:

//我对代码做了更多的清理:

代码语言:javascript
复制
import 'dart:async';

Duration d1 = new Duration(seconds: 5);
Duration d2 = new Duration(seconds: 10);
Duration d3 = new Duration(seconds: 15);

Future<bool> f1() {
  print('Entering f1');
  Completer<bool> r = new Completer();
  new Timer(d1, () {
    r.complete(true);
  });
  print('Leaving f1');
  return r.future;
}

Future<bool> f2() {
  print('Entering f2');
  Completer<bool> r = new Completer();
  new Timer(d2, () {
    r.complete(true);
  });
  print('Leaving f2');
  return r.future;
}

Future<bool> f3() {
  print('Entering f3');
  Completer<bool> r = new Completer();
  new Timer(d3, () {
    r.complete(true);
  });
  print('Leaving f3');
  return r.future;
}

void main() {
    print('Starting main');
    f1().then((_) {
        f2().then((_) {
            f3().then((_) {

            });
        });
    });
    print('Leaving main');
}
票数 2
EN

Stack Overflow用户

发布于 2014-12-15 07:00:06

在start1中,您返回一个从未完成的未来。基本上,你所做的是:

代码语言:javascript
复制
start1() {
  var r = new Completer();
  r.then.((_) { ... r.complete() ... });
  return r.future;
}

由于唯一完整的r需要完成r,所以不会发生这种情况。这意味着result of f1将永远等待start1返回的未来,而f1().then((_) ...将永远无法到达then回调。这就是为什么什么都没发生。

您也有一个繁忙的等待循环while (!r1) {}。如果您打算在r1不正确的情况下挂起它,那么它可以正常工作。期货不同时在Dart执行。在调用任何以后的回调之前,您必须返回到事件循环,因此如果r1在到达该循环时为false,则它将保持为false,因为没有其他代码会中断该循环。

如果我理解您对start1的要求,也许可以尝试将其重写为:

代码语言:javascript
复制
// Returns a future which completes when `r1` becomes true.
start1() {
  var r = new Completer();
  void check() {
    if (r1) {
      r.complete();
    } else {
      // We can't use scheduleMicrotask here, because an infinite microtask
      // loop would prevent the timer from firing.
      Timer.run(check);  // Check again later.
    }
  }
  check();
  return r.future;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27468003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档