everyday com-eat
작성일
2024. 1. 8. 21:08
작성자
갱수터

목차

    728x90

     

    async / await / Futre : 1회만 응답을 돌려받는 경우
    async* / yield / Stream : 지속적으로 응답을 돌려받는 경우

     

     

     

    Future<void> todo (int second) async {
    await Future.delayed(Duration(seconds: second));
    print('TODO Done in $second seconds');
    }
    todo(3); //3초뒤
    todo(1); //1초뒤
    todo(5); //5초뒤
    Stream<int> todo2() async* {
    int cnt = 0;
    while(cnt <= 10) {
    cnt++;
    await Future.delayed(Duration(seconds: 1));
    print('TODO is Running $cnt');
    yield cnt;
    }
    print('TODO is Done');
    }
    todo2().listen((event) {}); //10초 뒤 자동 종료