gpt4 book ai didi

dart - future 顺序

转载 作者:行者123 更新时间:2023-12-03 04:12:32 28 4
gpt4 key购买 nike

在下面的代码中,我认为f1 > f2 > f3是调用顺序,但是仅f1被调用。如何获得依次调用的3个函数?

我已经将以下内容添加到main函数中,它可以按预期工作,但是我想知道是否还有其他确定的方法可以达到相同的结果?

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');
}

最佳答案

首先,您应该阐明为什么需要此功能。我真的不明白为什么要让它们按顺序执行-您应该向我们提供更多有关其背后概念的信息。

请给我们更多有关您要完成的工作的信息!以下代码以某种方式做了我认为您想要的:

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

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');
}

关于dart - future 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27468003/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com