Search

๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

โ€ข
Future
โ€ข
async, await
โ€ข
Stream

Future

์ข…๋ฃŒ๋˜์ง€ ์•Š์€ ๋น„๋™๊ธฐ ์š”์ฒญ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํด๋ž˜์Šค
์š”์ฒญ ํ›„, ๊ฒฐ๊ณผ๋ฅผ ๋‚˜์ค‘์— ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
Future<๋ฐ˜ํ™˜ํƒ€์ž…> ๋ฉ”์†Œ๋“œ๋ช…() async { return ๋ฐ˜ํ™˜๊ฐ’; }
Dart
๋ณต์‚ฌ
// 5์ดˆ ์ง€์—ฐ Future<void> fetchOrder1() async { // Future.delayed( ์ง€์—ฐ, ์ฝœ๋ฐฑํ•จ์ˆ˜ ) Future.delayed(const Duration(seconds: 5), () { print('5์ดˆํ›„ ์ถœ๋ ฅ...'); }); } // 3์ดˆ ์ง€์—ฐ Future<void> fetchOrder2() async { // Future.delayed( ์ง€์—ฐ, ์ฝœ๋ฐฑํ•จ์ˆ˜ ) Future.delayed(const Duration(seconds: 3), () { print('3์ดˆํ›„ ์ถœ๋ ฅ...'); }); } void main(List<String> args) { fetchOrder1(); fetchOrder2(); print('์š”์ฒญ ์ค‘...'); }
Dart
๋ณต์‚ฌ
Future<String> fetchData() { return Future.delayed(Duration(seconds: 2), () { return '๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ'; }); } void fetchDataAwait() async { String result = await fetchData(); print(result); } void main(List<String> args) { // then(data) // : Future ๊ฐ€ ์™„๋ฃŒ๋œ ํ›„, ๋ฐ˜ํ™˜๊ฐ’ data ๋ฅผ ๊ฐ€์ ธ์™€์„œ, // ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ fetchData().then((data) { print(data); print('์ถ”๊ฐ€ ์ž‘์—…...'); }); fetchDataAwait(); print('์š”์ฒญ ์ค‘...'); }
Dart
๋ณต์‚ฌ

async, await

aysnc

ํ•จ์ˆ˜๊ฐ€ ๋น„๋™๊ธฐ๋กœ ์‹คํ–‰๋˜๋„๋ก ์ง€์ •ํ•˜๋Š” ํ‚ค์›Œ๋“œ
async ํ•จ์ˆ˜๋Š” ๋น„๋™๊ธฐ๋กœ ์‹คํ–‰๋˜๋Š” Future ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
Future<void> exampleAsyncFunction() async { // ๋น„๋™๊ธฐ ์ž‘์—… ์ˆ˜ํ–‰ }
Dart
๋ณต์‚ฌ

await

async ํ•จ์ˆ˜ ๋‚ด์—์„œ Future ์™„๋ฃŒ ์‹œ, ํ•จ์ˆ˜ ์‹คํ–‰์„ ๊ธฐ๋‹ค๋ ค์„œ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ‚ค์›Œ๋“œ
Future<void> exampleAsyncFunction() async { // ๋น„๋™๊ธฐ ์ž‘์—… ์ˆ˜ํ–‰ var result = await someAsyncOperation(); print(result); }
Dart
๋ณต์‚ฌ

Stream

๋น„๋™๊ธฐ ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค
Stream<int> countStream(int to) async* { for(int i = 1 ; i <= to ; i++ ){ await Future.delayed(Duration(seconds: 1), () { print('์š”์ฒญ : $i'); }); yield i; } } Future<int> sumStream(Stream<int> stream) async { var sum = 0; print('ํ•ฉ๊ณ„'); await for(final value in stream ) { sum += value; print('์ „๋‹ฌ๋œ ๊ฐ’ : $value - sum : $sum'); } return sum; } Future<void> main(List<String> args) async { var stream = countStream(10); var sum = await sumStream(stream); print('์š”์ฒญ์ค‘...'); print(sum); print(stream); }
Dart
๋ณต์‚ฌ

์ฝ”๋“œ ์‹คํ–‰ ํ๋ฆ„

1.
main ํ•จ์ˆ˜์—์„œ countStream(10)์„ ํ˜ธ์ถœํ•˜์—ฌ Stream ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
2.
sumStream ํ•จ์ˆ˜์— Stream์„ ์ „๋‹ฌํ•˜์—ฌ ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
3.
countStream ํ•จ์ˆ˜์—์„œ 1์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ ์ˆซ์ž๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ Stream์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
4.
sumStream ํ•จ์ˆ˜์—์„œ Stream์˜ ๊ฐ ์ˆซ์ž๋ฅผ ๋ฐ›์•„์„œ ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
5.
๋ชจ๋“  ์ˆซ์ž๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋ฉด ์ตœ์ข… ํ•ฉ๊ณ„๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
6.
stream ๊ฐ์ฒด์˜ ๋ฌธ์ž์—ด ํ‘œํ˜„์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
1. countStream ํ•จ์ˆ˜:
โ€ข
1๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๊นŒ์ง€ 1์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ ์ˆซ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” Stream์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
โ€ข
async* ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ ์ƒ์„ฑ๊ธฐ ํ•จ์ˆ˜๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
for ๋ฐ˜๋ณต๋ฌธ์—์„œ 1๋ถ€ํ„ฐ to๊นŒ์ง€ ์ˆซ์ž๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
โ€ข
Future.delayed๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 1์ดˆ ์ง€์—ฐ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
yield ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์ˆซ์ž๋ฅผ Stream์œผ๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
2. sumStream ํ•จ์ˆ˜:
โ€ข
Stream์„ ์ „๋‹ฌ๋ฐ›์•„์„œ ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
โ€ข
async ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ ํ•จ์ˆ˜๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
sum ๋ณ€์ˆ˜๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
await for ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Stream์˜ ๊ฐ ๊ฐ’์„ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.
โ€ข
๊ฐ ๊ฐ’์„ sum์— ๋”ํ•˜๊ณ , ํ˜„์žฌ ๊ฐ’๊ณผ ํ•ฉ๊ณ„ ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
โ€ข
์ตœ์ข… ํ•ฉ๊ณ„๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
3. main ํ•จ์ˆ˜:
โ€ข
countStream ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ 1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์ƒ์„ฑํ•˜๋Š” Stream์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
โ€ข
sumStream ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Stream์˜ ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
โ€ข
"์š”์ฒญ์ค‘..." ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
โ€ข
๊ณ„์‚ฐ๋œ ํ•ฉ๊ณ„๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
โ€ข
stream์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. (stream ๊ฐ์ฒด์˜ ๋ฌธ์ž์—ด ํ‘œํ˜„์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.)

์ถœ๋ ฅ ๊ฒฐ๊ณผ

ํ•ฉ๊ณ„ ์š”์ฒญ : 1 ์ „๋‹ฌ๋œ ๊ฐ’ : 1 - sum : 1 ์š”์ฒญ : 2 ์ „๋‹ฌ๋œ ๊ฐ’ : 2 - sum : 3 ... ์š”์ฒญ : 10 ์ „๋‹ฌ๋œ ๊ฐ’ : 10 - sum : 55 ์š”์ฒญ์ค‘... 55
Dart
๋ณต์‚ฌ