I'd like to kick off an HttpRequest and then poll for its completion. This feels like a fairly common pattern.
void main() {
_tick(0);
_tickB(0);
build();
print("End of main");
}
void build() {
var path = '../layout.txt';
html.HttpRequest.getString(path).then((String layout) {
print("layout received");
});
}
void _tick(int i) {
print("A $i");
if (i < 10) incr(i).then(_tick);
}
void _tickB(int i) {
print("B $i");
if (i < 10) incr(i).then(_tickB);
}
Future<int> incr(int i) async {
i++;
return i;
}
Output:
A1
B1
End of main
A2
B2
A3
B3
A4
B4
A5
B5
layout received
I would expect to see "layout received" appear interleaved with the other callbacks when the HttpRequest completes but its not. Its always (no matter the value of i, 5 is just a demo number) at the end of the interleaved callbacks.
Why is this?
How can I schedule the build callback in such a way that it completes during the other callback chains?