当前位置:网站首页>Async function implements multiple request processing
Async function implements multiple request processing
2022-07-18 15:38:00 【Mountains and seas dream】
Preface
async Function is actually
GeneratorFunctionalGrammatical sugar , As for it and GeneratorWhat's the difference? 、 What are the advantages 、 Basic usage 、 Semantics, etc. it will be easier to understand the document directly . This article is mainly about , How do we use if there are multiple requests on the homepage async Function processing
One 、 Use async Function to implement multiple requests
because async Function in awat After that, you can return Promise Value of object and original type ( The number 、 String and Boolean , But it will automatically change to immediate resolved Of Promise object ). So I like to define an object to accept await Returned data , as follows :
async initData() {
const cache = await this.cachePromise;
} reflection : What if there are multiple requests ? Join multiple await return promise Instance ? If async An exception inside the function will result in the return Promise The object becomes reject state , Later requests will wait, causing request congestion , So sometimes , We hope that even if the previous asynchronous operation fails , Do not interrupt the subsequent asynchronous operation . At this time, you can await Put it in try...catch Inside the structure , So whether or not the asynchronous operation succeeds , Back await It will be carried out .
async initData() {
const cache = await this.cachePromise; // Something went wrong
const cache2 = await this.cachePromise2; // Don't execute
const cache3 = await this.cachePromise3; // Don't execute
const cache4 = await this.cachePromise4; // Don't execute
}
// After modification
async initData() {
try {
const cache = await this.cachePromise; // Something went wrong
const cache2 = await this.cachePromise2; // perform
const cache3 = await this.cachePromise3; // perform
const cache4 = await this.cachePromise4; // perform
} catch(err) {
console.log(err)
}
}
Two 、 Use async Function to realize the concurrency of multiple requests
1. reflection
From the above code, we have to think about another problem , because cachePromise、cachePromise2、cachePromise3、cachePromise4 Asynchronous requests are independent of each other , But the above writing is followed by a request to wait await The next request will not begin until the request ends , It's time consuming .
2. Solution
adopt Promise.all Implement concurrent requests , They also trigger shortening the execution time of the program .
The code is as follows ( Example ):
Be careful :await Must be on async In the function
// Writing a
let [cache, cache2] = await Promise.all([this.cachePromise, this.cachePromise2]);
// Write two
let p= this.cachePromise();
let p2= this.cachePromise2();
let cache= await p;
let cache2= await p2;Reference resources :ES6 async function _w3cschool
summary
Sometimes it is not necessary to handle multiple request concurrency async Functions don't use async Just then Callback , It mainly depends on personal habits . I think the use of async Function is very convenient. It can directly request the return data , It's very delicious to be able to do prompt processing for some form submissions . I have a chicken dish, please give me more advice
边栏推荐
- The bottom logic of learning essence
- 1111111111111
- Ciphertext on RGB image -- illegal data hiding
- The application of Lora and lorawan wireless technology in the Internet of things
- 面试常问的shell基础,你会了吗?
- Hybridclr -- epoch-making unity native C # hot update technology
- 移远通信助力夏粮存储新招式,科技手段更有效
- Calculate the Euclidean distance between the row vectors of two matrices
- Vulnhub-dc5 learning notes
- The difference and use between get request and post request
猜你喜欢

基于NvidiaGPU的AI模型结构优化

Detailed explanation of Flink resource management

What is a recommendation system?

How long can zynq PL interrupt pulses be captured by CPU

Comprehensive evaluation method

Distributed notes (01) - theory and principle of distributed cap

Detailed explanation of C language "address book"
![[dynamic memory allocation]](/img/29/ef88425f0f1b6e775793b6e51c2170.png)
[dynamic memory allocation]

VS2019 16.8 “消失“的团队资源管理器

高性能定時器
随机推荐
El input number disable scientific counting without result, and replace it with El input (type= "number")
高性能定时器
Architecture Basics
cv2.setMouseCallback()显示鼠标点击图片的像素值和位置
Mipi c-phy popular science
ESRI launches indoor positioning system for facility routing
CV2. Setmousecallback() displays the pixel value and position of the mouse click image
【LeetCode】11. Lowest common ancestor of a binary search tree
Operation of ES
Processing TCP and UDP services simultaneously with i/o multiplexing
redis 配置,集群安装与扩容
ONNX模型tensor shapes inference和Flops统计工具
[dynamic memory allocation]
计算两个矩阵的行向量之间的欧式距离
通过制定愿景克服数字化转型挑战
RS485接口OSI模型的应用层
JS Object.keys()
The difference and use between get request and post request
从小米10发布来看编译优化
Vulnhub-dc5 learning notes