Python đang chờ trong câu lệnh if

Sử dụng câu lệnh

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
68 để chỉ định một khối mã JavaScript sẽ được thực thi nếu một điều kiện là đúng

cú pháp

if [điều kiện] {
  //  khối mã sẽ được thực thi nếu điều kiện là đúng
}

Lưu ý rằng

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
68 là chữ thường. Chữ in hoa [If hoặc IF] sẽ tạo ra lỗi JavaScript

Thí dụ

Thực hiện lời chúc "Chúc một ngày tốt lành" nếu chưa đến 18 giờ. 00

if [giờ < 18] {
  lời chào = "Chúc một ngày tốt lành";
}

Kết quả của lời chào sẽ là

Tự mình thử »

Tuyên bố khác

Sử dụng câu lệnh

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
70 để chỉ định một khối mã sẽ được thực thi nếu điều kiện sai

if [điều kiện] {
  //  khối mã sẽ được thực thi nếu điều kiện đúng
} else {
  //  block of code to be executed if the condition is false
}

Thí dụ

Nếu chưa đến 18 giờ thì tạo lời chào "Chúc một ngày tốt lành", nếu không thì "Chào buổi tối"

if [hour < 18] {
  greeting = "Chúc một ngày tốt lành";
} other {
  lời chào
}

Kết quả của lời chào sẽ là

Tự mình thử »

Câu lệnh if khác

Sử dụng câu lệnh

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
71 để chỉ định một điều kiện mới nếu điều kiện đầu tiên là sai

cú pháp

if [condition1] {
  //  khối mã sẽ được thực thi nếu điều kiện 1 đúng
} else if [condition2] {
  //  block of code to be executed if the condition1 is false and condition2 is true
} else {
  //  block of code to be executed if the condition1 is false and condition2 is false
}

Thí dụ

Nếu thời gian nhỏ hơn 10. 00 thì tạo lời chào "Chào buổi sáng", nếu chưa có nhưng thời gian dưới 20. 00, tạo lời chào "Chúc một ngày tốt lành", nếu không là "Chào buổi tối"

Lớp học lập trình này hướng dẫn bạn cách viết mã không đồng bộ bằng cách sử dụng hợp đồng tương lai và các từ khóa

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7. Sử dụng trình chỉnh sửa DartPad nhúng, bạn có thể kiểm tra kiến ​​thức của mình bằng cách chạy mã ví dụ và hoàn thành bài tập

Để tận dụng tối đa codelab này, bạn nên có những điều sau đây

  • Kiến thức về cú pháp Dart cơ bản
  • Một số kinh nghiệm viết mã không đồng bộ bằng ngôn ngữ khác

Codelab này bao gồm các tài liệu sau

  • Cách thức và thời điểm sử dụng các từ khóa
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6 và
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    7
  • Việc sử dụng
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6 và
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    7 ảnh hưởng đến thứ tự thực hiện như thế nào
  • Cách xử lý lỗi từ lệnh gọi không đồng bộ bằng cách sử dụng biểu thức
    void main[] async { ··· }
    2 trong hàm
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6

Thời gian ước tính để hoàn thành phòng thí nghiệm mã này. 40-60 phút

Ghi chú. Trang này sử dụng DartPads được nhúng để hiển thị các ví dụ và bài tập. Nếu bạn thấy các ô trống thay vì DartPad, hãy truy cập trang xử lý sự cố DartPad

Tại sao mã không đồng bộ lại quan trọng

Hoạt động không đồng bộ cho phép chương trình của bạn hoàn thành công việc trong khi chờ hoạt động khác kết thúc. Dưới đây là một số hoạt động không đồng bộ phổ biến

  • Tìm nạp dữ liệu qua mạng
  • Ghi vào cơ sở dữ liệu
  • Đọc dữ liệu từ một tập tin

Các tính toán không đồng bộ như vậy thường cung cấp kết quả dưới dạng

void main[] async { ··· }
4 hoặc, nếu kết quả có nhiều phần, dưới dạng
void main[] async { ··· }
5. Những tính toán này giới thiệu sự không đồng bộ vào một chương trình. Để phù hợp với sự không đồng bộ ban đầu đó, các hàm Dart đơn giản khác cũng cần trở nên không đồng bộ

Để tương tác với các kết quả không đồng bộ này, bạn có thể sử dụng từ khóa

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7. Hầu hết các hàm không đồng bộ chỉ là các hàm Dart không đồng bộ, có thể phụ thuộc sâu vào tính toán không đồng bộ vốn có

Thí dụ. Sử dụng sai chức năng không đồng bộ

Ví dụ sau đây cho thấy cách sai khi sử dụng hàm không đồng bộ [_______68]. Sau này bạn sẽ sửa ví dụ bằng cách sử dụng

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7. Trước khi chạy ví dụ này, hãy thử phát hiện vấn đề – bạn nghĩ đầu ra sẽ như thế nào?

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
5

Đây là lý do tại sao ví dụ không in được giá trị mà

void main[] async { ··· }
8 cuối cùng tạo ra

  • void main[] async { ··· }
    8 là một hàm không đồng bộ, sau một khoảng thời gian trễ, sẽ cung cấp một chuỗi mô tả đơn đặt hàng của người dùng. một “Latte lớn”
  • Để nhận được đơn đặt hàng của người dùng, ________ 183 nên gọi cho ________ 68 và đợi kết thúc. Vì
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    83 không đợi cho đến khi
    void main[] async { ··· }
    8 kết thúc, nên
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    83 không nhận được giá trị chuỗi mà cuối cùng
    void main[] async { ··· }
    8 đã cung cấp
  • Thay vào đó,
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    83 nhận được một đại diện của công việc đang chờ xử lý được thực hiện. một tương lai chưa hoàn thành. Bạn sẽ tìm hiểu thêm về tương lai trong phần tiếp theo
  • Because
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    83 fails to get the value describing the user’s order, the example fails to print “Large Latte” to the console, and instead prints “Your order is: Instance of ‘_Future’”.

Trong các phần tiếp theo, bạn sẽ tìm hiểu về hợp đồng tương lai và cách làm việc với hợp đồng tương lai [sử dụng

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7] để bạn có thể viết mã cần thiết để làm cho
void main[] async { ··· }
8 in giá trị mong muốn [“Latte Latte lớn”] vào bảng điều khiển

Điều khoản quan trọng

  • hoạt động đồng bộ. Một hoạt động đồng bộ chặn các hoạt động khác thực thi cho đến khi nó hoàn thành
  • chức năng đồng bộ. Hàm đồng bộ chỉ thực hiện các hoạt động đồng bộ
  • hoạt động không đồng bộ. Sau khi bắt đầu, một hoạt động không đồng bộ cho phép các hoạt động khác thực thi trước khi hoàn thành
  • chức năng không đồng bộ. Hàm không đồng bộ thực hiện ít nhất một thao tác không đồng bộ và cũng có thể thực hiện các thao tác đồng bộ

Tương lai là gì?

Tương lai [viết thường “f”] là một thể hiện của lớp Tương lai [viết hoa “F”]. Một tương lai đại diện cho kết quả của một hoạt động không đồng bộ và có thể có hai trạng thái. chưa hoàn thành hoặc đã hoàn thành

Ghi chú. Chưa hoàn thành là một thuật ngữ Dart đề cập đến trạng thái của một tương lai trước khi nó tạo ra một giá trị

chưa hoàn thành

Khi bạn gọi một hàm không đồng bộ, nó sẽ trả về một tương lai chưa hoàn thành. Tương lai đó đang chờ hoạt động không đồng bộ của chức năng kết thúc hoặc đưa ra lỗi

Hoàn thành

Nếu hoạt động không đồng bộ thành công, tương lai sẽ hoàn thành với một giá trị. Mặt khác, nó hoàn thành với một lỗi

Hoàn thành với một giá trị

Một tương lai của loại

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
94 hoàn thành với một giá trị của loại
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
95. Ví dụ: một tương lai với loại
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
96 tạo ra một giá trị chuỗi. Nếu một tương lai không tạo ra giá trị sử dụng được, thì loại tương lai là
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
97

Hoàn thành với một lỗi

Nếu hoạt động không đồng bộ được thực hiện bởi chức năng không thành công vì bất kỳ lý do gì, tương lai sẽ hoàn thành với một lỗi

Thí dụ. giới thiệu tương lai

Trong ví dụ sau,

void main[] async { ··· }
8 trả về một tương lai hoàn thành sau khi in ra bàn điều khiển. Bởi vì nó không trả về giá trị có thể sử dụng, nên
void main[] async { ··· }
8 có kiểu
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
97. Trước khi bạn chạy ví dụ, hãy thử dự đoán cái nào sẽ in trước. “Latte Latte lớn” hoặc “Đang tìm nạp đơn hàng của người dùng…”

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6

Trong ví dụ trước, mặc dù

void main[] async { ··· }
8 thực thi trước lệnh gọi
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
82 trên dòng 8, bảng điều khiển hiển thị đầu ra từ dòng 8[“Tìm nạp đơn đặt hàng của người dùng…”] trước đầu ra từ
void main[] async { ··· }
8 [“Latte Latte lớn”]. Điều này là do sự chậm trễ của
void main[] async { ··· }
8 trước khi nó in “Large Latte”

Thí dụ. Hoàn thành với một lỗi

Chạy ví dụ sau để xem cách một tương lai hoàn thành có lỗi. Một lúc sau, bạn sẽ học cách xử lý lỗi

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}

Trong ví dụ này,

void main[] async { ··· }
8 hoàn tất với lỗi cho biết ID người dùng không hợp lệ

Bạn đã học về hợp đồng tương lai và cách chúng hoàn thành, nhưng bạn sử dụng kết quả của các hàm không đồng bộ như thế nào?

đánh giá nhanh

  • A Future instance produces a value of type
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    95.
  • Nếu một tương lai không tạo ra giá trị sử dụng được, thì loại tương lai là
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    97
  • Một tương lai có thể ở một trong hai trạng thái. chưa hoàn thành hoặc đã hoàn thành
  • Khi bạn gọi một hàm trả về một tương lai, hàm này sẽ xếp hàng công việc cần hoàn thành và trả về một tương lai chưa hoàn thành
  • Khi hoạt động của tương lai kết thúc, tương lai hoàn thành với một giá trị hoặc có lỗi

Điều khoản quan trọng

  • Tương lai. lớp phi tiêu tương lai
  • Tương lai. một thể hiện của lớp Dart
    void main[] async { ··· }
    4

Làm việc với tương lai. không đồng bộ và chờ đợi

Các từ khóa

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7 cung cấp một cách khai báo để xác định các hàm không đồng bộ và sử dụng kết quả của chúng. Hãy nhớ hai hướng dẫn cơ bản này khi sử dụng
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7

  • Để xác định hàm không đồng bộ, hãy thêm
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6 trước thân hàm
  • Từ khóa
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    7 chỉ hoạt động trong các hàm
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6

Đây là một ví dụ chuyển đổi

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
58 từ chức năng đồng bộ sang không đồng bộ

Đầu tiên, thêm từ khóa

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 trước thân hàm

void main[] async { ··· }

Nếu hàm có kiểu trả về được khai báo, thì hãy cập nhật kiểu đó thành

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
94, trong đó
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
95 là kiểu giá trị mà hàm trả về. Nếu hàm không trả về một giá trị rõ ràng, thì kiểu trả về là
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
97

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
8

Bây giờ bạn đã có hàm

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6, bạn có thể sử dụng từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7 để đợi một tương lai hoàn thành

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
9

Như hai ví dụ sau cho thấy, các từ khóa

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7 dẫn đến mã không đồng bộ trông rất giống mã đồng bộ. Sự khác biệt duy nhất được đánh dấu trong ví dụ không đồng bộ, mà—nếu cửa sổ của bạn đủ rộng—nằm ở bên phải của ví dụ đồng bộ

Thí dụ. chức năng đồng bộ

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
8

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
5

Thí dụ. chức năng không đồng bộ

void main[] async { ··· }
9
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
0

Ví dụ không đồng bộ khác nhau theo ba cách

  • Kiểu trả về cho
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    83 thay đổi từ
    void main[] async { ··· }
    98 thành
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    96
  • Từ khóa
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6 xuất hiện trước thân hàm cho
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    83 và
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    58
  • Từ khóa
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    7 xuất hiện trước khi gọi các hàm không đồng bộ
    void main[] async { ··· }
    8 và
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    83

Điều khoản quan trọng

  • không đồng bộ. Bạn có thể sử dụng từ khóa
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6 trước phần thân của hàm để đánh dấu nó là không đồng bộ
  • chức năng không đồng bộ. Hàm
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6 là một hàm được gắn nhãn bằng từ khóa
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6
  • chờ đợi. Bạn có thể sử dụng từ khóa
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    7 để lấy kết quả hoàn thành của biểu thức không đồng bộ. Từ khóa
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    7 chỉ hoạt động trong hàm
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    6

Luồng thực thi với async và đang chờ

Một hàm

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 chạy đồng bộ cho đến từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7 đầu tiên. Điều này có nghĩa là trong thân hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6, tất cả mã đồng bộ trước từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7 đầu tiên sẽ thực thi ngay lập tức

Thí dụ. Thực thi trong các chức năng không đồng bộ

Chạy ví dụ sau để xem quá trình thực thi diễn ra như thế nào trong thân hàm

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6. Bạn nghĩ đầu ra sẽ là gì?

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
60

Sau khi chạy mã trong ví dụ trước, hãy thử đảo ngược dòng 2 và 3

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
61

Lưu ý rằng thời gian của đầu ra thay đổi, bây giờ

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
607 xuất hiện sau từ khóa
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7 đầu tiên trong
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
609

Tập thể dục. Thực hành sử dụng async và chờ đợi

Bài tập sau đây là một bài kiểm tra đơn vị không thành công có chứa các đoạn mã đã hoàn thành một phần. Nhiệm vụ của bạn là hoàn thành bài tập bằng cách viết mã để vượt qua bài kiểm tra. Bạn không cần triển khai

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
58

Để mô phỏng các hoạt động không đồng bộ, hãy gọi các chức năng sau, được cung cấp cho bạn

FunctionType signatureDescriptionfetchRole[]
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
611Nhận mô tả ngắn về vai trò của người dùng. fetchLoginAmount[]
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
612Lấy số lần người dùng đã đăng nhập

Phần 1.
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
613

Thêm mã vào hàm

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
613 để nó thực hiện như sau

  • Trả về một tương lai hoàn thành với chuỗi sau.
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    615
    • Ghi chú. Bạn phải sử dụng giá trị thực được trả về bởi
      Future fetchUserOrder[] {
      // Imagine that this function is fetching user info but encounters a bug
        return Future.delayed[const Duration[seconds: 2],
            [] => throw Exception['Logout failed: user ID is invalid']];
      }
      
      void main[] {
        fetchUserOrder[];
        print['Fetching user order...'];
      }
      616;
    • Giá trị trả về ví dụ.
      Future fetchUserOrder[] {
      // Imagine that this function is fetching user info but encounters a bug
        return Future.delayed[const Duration[seconds: 2],
            [] => throw Exception['Logout failed: user ID is invalid']];
      }
      
      void main[] {
        fetchUserOrder[];
        print['Fetching user order...'];
      }
      617
  • Nhận vai trò người dùng bằng cách gọi hàm được cung cấp
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    616

Phần 2.
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
619

Triển khai hàm

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
619 để nó thực hiện như sau

  • Trả về chuỗi
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    622
    • Ghi chú. Bạn phải sử dụng giá trị thực được trả về bởi
      Future fetchUserOrder[] {
      // Imagine that this function is fetching user info but encounters a bug
        return Future.delayed[const Duration[seconds: 2],
            [] => throw Exception['Logout failed: user ID is invalid']];
      }
      
      void main[] {
        fetchUserOrder[];
        print['Fetching user order...'];
      }
      623;
    • Giá trị trả về ví dụ từ
      Future fetchUserOrder[] {
      // Imagine that this function is fetching user info but encounters a bug
        return Future.delayed[const Duration[seconds: 2],
            [] => throw Exception['Logout failed: user ID is invalid']];
      }
      
      void main[] {
        fetchUserOrder[];
        print['Fetching user order...'];
      }
      619.
      Future fetchUserOrder[] {
      // Imagine that this function is fetching user info but encounters a bug
        return Future.delayed[const Duration[seconds: 2],
            [] => throw Exception['Logout failed: user ID is invalid']];
      }
      
      void main[] {
        fetchUserOrder[];
        print['Fetching user order...'];
      }
      625
  • Nhận số lần đăng nhập bằng cách gọi hàm được cung cấp
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    623

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
62

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
63

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
64

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
65

Ghi chú. Nếu mã của bạn vượt qua các bài kiểm tra, bạn có thể bỏ qua các thông báo cấp thông tin

Xử lý lỗi

Để xử lý lỗi trong hàm

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6, hãy sử dụng try-catch

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
66

Trong một hàm

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6, bạn có thể viết các mệnh đề try-catch giống như cách bạn viết trong mã đồng bộ

Thí dụ. không đồng bộ và chờ đợi với try-catch

Chạy ví dụ sau để xem cách xử lý lỗi từ hàm không đồng bộ. Bạn nghĩ đầu ra sẽ là gì?

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
67

Tập thể dục. Thực hành xử lý lỗi

Bài tập sau đây cung cấp cách thực hành xử lý lỗi với mã không đồng bộ, sử dụng cách tiếp cận được mô tả trong phần trước. Để mô phỏng các hoạt động không đồng bộ, mã của bạn sẽ gọi hàm sau, hàm này được cung cấp cho bạn

Chữ ký loại chức năngDescriptionfetchNewUsername[]
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
629Trả lại tên người dùng mới mà bạn có thể sử dụng để thay thế tên người dùng cũ

Sử dụng

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
6 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
7 để triển khai hàm
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
632 không đồng bộ thực hiện như sau

  • Gọi hàm không đồng bộ được cung cấp
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    633 và trả về kết quả của nó
    • Giá trị trả về ví dụ từ
      Future fetchUserOrder[] {
      // Imagine that this function is fetching user info but encounters a bug
        return Future.delayed[const Duration[seconds: 2],
            [] => throw Exception['Logout failed: user ID is invalid']];
      }
      
      void main[] {
        fetchUserOrder[];
        print['Fetching user order...'];
      }
      632.
      Future fetchUserOrder[] {
      // Imagine that this function is fetching user info but encounters a bug
        return Future.delayed[const Duration[seconds: 2],
            [] => throw Exception['Logout failed: user ID is invalid']];
      }
      
      void main[] {
        fetchUserOrder[];
        print['Fetching user order...'];
      }
      635
  • Bắt bất kỳ lỗi nào xảy ra và trả về giá trị chuỗi của lỗi
    • Bạn có thể sử dụng phương thức toString[] để xâu chuỗi cả Ngoại lệ và Lỗi

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
68

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
69

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
0

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
1

Tập thể dục. Để tất cả chúng cùng nhau

Đã đến lúc thực hành những gì bạn đã học trong một bài tập cuối cùng. Để mô phỏng các hoạt động không đồng bộ, bài tập này cung cấp các chức năng không đồng bộ

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
636 và
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
637

Chữ ký loại chức năngDescriptionfetchUsername[]
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
638Trả về tên được liên kết với người dùng hiện tại. logoutUser[]
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
639Thực hiện đăng xuất của người dùng hiện tại và trả lại tên người dùng đã đăng xuất

Viết như sau

Phần 1.
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
640

  • Viết một hàm
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    640 nhận một đối số duy nhất là
    void main[] async { ··· }
    98
  • ______3640 trả về đối số
    void main[] async { ··· }
    98 của nó trước
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    645.
    Ví dụ.
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    646 trả về
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    647.

Phần 2.
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
648

  • Viết hàm
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    648 không nhận đối số
  • Để lấy tên người dùng,
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    648 gọi hàm không đồng bộ được cung cấp
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    636
  • Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    648 tạo lời chào cho người dùng bằng cách gọi
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    640, chuyển tên người dùng và trả về kết quả.
    Ví dụ. Nếu
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    636 trả về
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    655, thì
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    648 trả về
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    657.

Phần 3.
Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
658

  • Viết hàm
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    658 thực hiện các công việc sau
    • Không có đối số
    • Bắt bất kỳ lỗi nào
    • Gọi hàm không đồng bộ được cung cấp
      Future fetchUserOrder[] {
      // Imagine that this function is fetching user info but encounters a bug
        return Future.delayed[const Duration[seconds: 2],
            [] => throw Exception['Logout failed: user ID is invalid']];
      }
      
      void main[] {
        fetchUserOrder[];
        print['Fetching user order...'];
      }
      637
  • Nếu
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    637 không thành công,
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    658 trả về bất kỳ chuỗi nào bạn muốn
  • Nếu
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    637 thành công,
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    658 trả về chuỗi
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    665, trong đó
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    666 là giá trị chuỗi được trả về bằng cách gọi
    Future fetchUserOrder[] {
    // Imagine that this function is fetching user info but encounters a bug
      return Future.delayed[const Duration[seconds: 2],
          [] => throw Exception['Logout failed: user ID is invalid']];
    }
    
    void main[] {
      fetchUserOrder[];
      print['Fetching user order...'];
    }
    637

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
2

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
3

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
4

Future fetchUserOrder[] {
// Imagine that this function is fetching user info but encounters a bug
  return Future.delayed[const Duration[seconds: 2],
      [] => throw Exception['Logout failed: user ID is invalid']];
}

void main[] {
  fetchUserOrder[];
  print['Fetching user order...'];
}
5

Cái gì tiếp theo?

Xin chúc mừng, bạn đã hoàn thành bài học lập trình. Nếu bạn muốn tìm hiểu thêm, sau đây là một số gợi ý về địa điểm tiếp theo

  • Chơi với DartPad
  • Hãy thử một phòng thí nghiệm lập trình khác
  • Tìm hiểu thêm về tương lai và không đồng bộ
    • hướng dẫn luồng. Tìm hiểu cách làm việc với chuỗi sự kiện không đồng bộ
    • Đồng thời trong Dart Hiểu và tìm hiểu cách triển khai đồng thời trong Dart
    • Video phi tiêu từ Google. Xem một hoặc nhiều video về mã hóa không đồng bộ. Hoặc, nếu bạn thích, hãy đọc các bài báo dựa trên các video này. [Bắt đầu với các vòng lặp cô lập và sự kiện. ]
  • Nhận SDK phi tiêu

Nếu bạn quan tâm đến việc sử dụng DartPad nhúng, giống như phòng thí nghiệm lập trình này, hãy xem các phương pháp hay nhất để sử dụng DartPad trong hướng dẫn

Làm thế nào để chờ đợi hoạt động trong Python?

Từ khóa không đồng bộ/đang chờ . Từ khóa async được sử dụng để tạo một coroutine Python. Từ khóa chờ tạm dừng việc thực thi một quy trình đăng ký cho đến khi quy trình đó hoàn thành và trả về dữ liệu kết quả . Các từ khóa chờ đợi chỉ hoạt động trong chức năng không đồng bộ.

Khi nào tôi nên sử dụng đang chờ Python?

Lý do chính để sử dụng async/await là để cải thiện thông lượng của chương trình bằng cách giảm lượng thời gian nhàn rỗi khi thực hiện I/O . Các chương trình với toán tử này đang ngầm sử dụng một trừu tượng được gọi là vòng lặp sự kiện để sắp xếp nhiều đường dẫn thực thi cùng một lúc.

Có khối chờ trong Python không?

Trong Python, chúng ta cần một từ khóa await trước mỗi đối tượng coroutine để nó được gọi bởi vòng lặp sự kiện. Nhưng khi chúng ta đặt await , nó sẽ chặn cuộc gọi . Theo đó, cuối cùng chúng tôi cũng làm điều tương tự như chúng tôi làm theo kiểu chặn.

Chờ đợi có mang lại Python không?

Câu trả lời OP chính xác. Không, await [per se] không mang lại lợi nhuận cho vòng lặp sự kiện , mang lại lợi nhuận cho vòng lặp sự kiện, do đó đối với trường hợp đã cho. "[2] Chúng tôi nhảy thẳng vào send_message ".

Chủ Đề