当前位置:网站首页>How async await implements concurrency
How async await implements concurrency
2022-07-28 11:52:00 【The king of the scroll goes to war】
I still remember my internship in autumn last year , The second interviewer, brother long ( The current team leader ) Asked an interview question ,async await How to achieve concurrency ? I suddenly remembered tonight , Hurry up and tidy up .
give an example
var fs=require('fs');
var read=function (path) {
return new Promise((resolve,reject)=>{
fs.readFile(path,(err,data)=>{
if(err){
reject(err)
}else{
resolve(data)
}
})
})
}
// Secondary read
async function ReadTwo(){
var f1=await read('./a.txt');
var f2=await read('./b.txt');
}
Solution
(1) promise.all()
async function readTwo{
var p=Promise.all([read('./a.txt'),read('./b.txt')]);
var [f1,f2]=await p
}
because promise.all It's concurrency , So these two requests are really concurrent
(2) Can't describe , Look at the code
async function readTwo{
var p1=read('./a.txt');
var p2=read('./a.txt');
f1=await p1;
f2=await p2;
}
These two requests are really concurrent , Because they are asynchronous functions , However, the assignment after obtaining the result is secondary
It's easy to think about it now , Not at that time .
边栏推荐
- Unity鼠标带动物体运动的三种方法
- What is the process of switching c read / write files from user mode to kernel mode?
- Globalthis is not defined solution
- echo -ne(echo line)
- R language ggplot2 visualization: ggdensity function of ggpubr package visualizes density graph and uses stat_ overlay_ normal_ Density function superimposes positive distribution curve, custom config
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the add parameter, add violin graph to the dot plot, and add the vertical line of mean standar
- PHP detects whether the URL URL link is normally accessible
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize the grouped dot plot, set the palette parameter, and set the color of data points in different grouped dot p
- Ripro9.0 revised and upgraded version +wp two beautification packages + rare plug-ins
- Introduction to the usage of SAP ui5 image display control avatar trial version
猜你喜欢

数字孪生轨道交通:“智慧化”监控疏通城市运行痛点

Upgrading of computing power under the coordination of software and hardware, redefining productivity

保障邮箱安全,验证码四个优势

Redis安装

Router firmware decryption idea
![[极客大挑战 2019]BabySQL-1|SQL注入](/img/21/b5b4727178a585e610d743e92248f7.png)
[极客大挑战 2019]BabySQL-1|SQL注入

Function of interface test

Five Ali technical experts have been offered. How many interview questions can you answer

Consumer installation and configuration
![[geek challenge 2019] babysql-1 | SQL injection](/img/21/b5b4727178a585e610d743e92248f7.png)
[geek challenge 2019] babysql-1 | SQL injection
随机推荐
Today's sleep quality record 74 points
Know the optical fiber interface and supporting optical fiber cable of can optical fiber converter in fire alarm networking
Thinkphp5 behavior hook return result (data) example
Router firmware decryption idea
数字孪生轨道交通:“智慧化”监控疏通城市运行痛点
Outlook suddenly becomes very slow and too laggy. How to solve it
Database advanced learning notes - system package
Why does acid food hurt teeth + early periodontitis
Design and implementation of SSM personal blog system
Update dev (development version) of the latest win11
Zotero document manager and its use posture (updated from time to time)
Jupiter、spyder、Anaconda Prompt 、navigator 快捷键消失的解决办法
Embrace open source guidelines
Consumer installation and configuration
MySQL (version 8.0.16) command and description
Database advanced learning notes - storage structure
15. User web layer services (III)
301. Delete invalid brackets
DNS series (III): how to avoid DNS spoofing
[pyGame practice] when the end of the world comes, how long can you live in a cruel survival game that really starts from scratch?