Last active
November 17, 2020 07:16
-
-
Save tahlin-notes/979b18562454daf73feac58d1a468ecd to your computer and use it in GitHub Desktop.
Sample Future chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
final sampleFuture = Future<void>(() { | |
print('2'); | |
}).then((value) { | |
print('3'); | |
}).whenComplete(() { | |
// Chú ý: whenComplete này thuộc về `Future()` được trả về bởi `then()` chứ không phải thuộc về `sampleFuture` | |
print('4'); | |
}).then((value) { | |
print('5'); | |
}); | |
print('1'); | |
sampleFuture.then((value) { | |
print('6'); | |
// Chú ý: Xử lý thứ 6 này không được gọi trong event đầu tiên đi cùng với `main()` mà được gọi trong event thứ 6 được đẩy vào event-queue thông qua `Future<T>` | |
// Vậy nên hàm print 8 và 9 sẽ được gọi sau 7 | |
sampleFuture.then((value) { | |
print('8'); | |
}).whenComplete(() { | |
print('9'); | |
}); | |
}).whenComplete(() { | |
print('7'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment