当前位置:网站首页>[fluent special topic] 91 illustration of future (II) yyds dry goods inventory of dart single thread asynchronous processing
[fluent special topic] 91 illustration of future (II) yyds dry goods inventory of dart single thread asynchronous processing
2022-04-22 13:34:00 【A Ze little monk】
Xiaocai just learned a few days ago Future Part of the way to implement asynchronous operations , It mainly includes the construction method and the commonly used static method ; Xiaocai continues to learn today Future Other knowledge and async-await Mode to implement asynchronous operation ;
Future nesting
The side dish was not made in the last blog Future Nesting attempts , There are many scenarios that require multiple asynchronous processing , And each asynchrony requires the result returned by the last asynchrony then() Then you can continue , You can use Future Nesting ; But if there are many hidden methods, it may affect the future code maintenance , That is to say Callback hell;


async-await
Future It can also be done through async-await Implement asynchronous operations ; It is usually used in multiple scenarios Future In series , Multi level nesting results in Callback hell, Use async-await Achieve asynchrony ;
async
async The asynchronous method used to decorate eventually encapsulates the return value as Future object ;
await
await Will automatically put the method into the blocking state , Wait until the task is completed and return the corresponding value ;
Case study
I tried the basic async-await usage ;
- The side dish is not used async and await key word , here Future.delayed() Back to a Future object , Can't get the returned data synchronously ;

2. The side dish only adds async key word , The method is modified as Asynchronous methods , Still can't get the returned data directly

3. The side dish has been added async and await Two key words , The compiler will eventually convert it into a Promise(Future) The call chain of , You can get the return result after asynchronous completion ; here Future Cannot set then() Callback methods, etc ;

- Try to use only await, Prompt at this time The await expression can only used in an async function,await Can only be used in async Methods the internal ;
- use async-await When the way , For exception capture , Can pass Future.catchError() To deal with it , You can also use the most commonly used try-catch-finally The way , Simple understanding of the corresponding then()-catchError()-whenComplete();

6. For multiple Future Nesting leads to Callback hell,async-await The way to deal with it should be concise ;

Small expansion
The side dish is trying async-await When I met async*, Where is the side dish bloc State management is always used in async* and Stream, Take a quick look at the differences ;
async*
async* It can also be used asynchronously , Use before method async* Keyword to mark the method as an asynchronous generator , Back to a Stream object , Use yield Statement to pass the value ;
about Stream Use , I have a basic understanding before the course , Usually by skin Add data , adopt listen Data monitoring ;
yield The key word is going to async* The output stream of the declared one-step generator adds a value , It's kind of similar return, It does not terminate the function ;


It's a piece of cake Dart Asynchronous cognition is not complete yet , I'll continue to try isolate as well as EventLoop Execution order, etc ; If there are mistakes and omissions, please give more guidance !
source : Little monk aze
版权声明
本文为[A Ze little monk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221135476557.html
边栏推荐
- no main manifest attribute / .jar中没有主清单属性
- Citrix SQL数据如何进行多表联查
- 算法---设计推特(Kotlin)
- XML external entity attack principle and Practice (xxE) (1)
- Summary of maximum matching number, minimum path coverage number, maximum independent number and minimum point coverage number theorems
- Données du panel sur la productivité verte totale des facteurs du pfgt par province (2004 - 2018)
- How does MySQL sort by default when using the select statement without order by?
- Fizz enterprise microservice Gateway - service choreography, offering a big killer to end the BFF layer
- [introduction to keras] MNIST dataset classification
- Redis persistence
猜你喜欢
随机推荐
SPI协议的通信原理
vscode 数据库调用存储过程,出现Unreconized data
HDU 2544 Dijkstra(模板)
Alibaba cloud changes its commander and competes for Huawei's territory
自动化测试的生命周期是什么?
Originally, this is the correct posture for developers to open world book day!
Algorithm - Design twitter (kotlin)
VMware虚拟机克隆后NAT模式下网络的配置
MapReduce案例-关于流量统计的求和分区规约排序操作
微积分之函数极限
MapReduce案例-上行流量倒序排序(递减排序)
HDU 2680 shortest path Dijkstra + chained forward star + priority queue (template)
Oplg: new generation cloud primary observable best practices
Knowledge to be learned
美女同事的烦恼:如何配置 Apache SkyWalking 告警?
Overview and deployment of redis (master-slave replication, sentinel mode and cluster)
The "open source summer" activity is hot. In the registration, rich bonuses are waiting for you to get
Redis persistence
Xen thermal repair technology (basic understanding)
Harbor V2. 5 Mise à jour, quelles fonctions ont été ajoutées?









