当前位置:网站首页>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 .
边栏推荐
- 【补题日记】[2022牛客暑期多校2]H-Take the Elevator
- In order to ensure the normal operation of fire-fighting equipment in large buildings, the power supply monitoring system of fire-fighting equipment plays a key role
- Six papers that must be seen in the field of target detection
- Client service registration of Nacos registry
- 移动端人脸风格化技术的应用
- ASP. Net core 6 framework unveiling example demonstration [29]: building a file server
- Software testing and quality learning notes 1 --- black box testing
- Service Workers让网站动态加载Webp图片
- 301. Delete invalid brackets
- SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
猜你喜欢
![[geek challenge 2019] babysql-1 | SQL injection](/img/21/b5b4727178a585e610d743e92248f7.png)
[geek challenge 2019] babysql-1 | SQL injection

LabVIEW AI visual Toolkit (non Ni vision) download and installation tutorial

Globalthis is not defined solution

对话庄表伟:开源第一课

Four advantages of verification code to ensure mailbox security

WPF layout controls are scaled up and down with the window, which is suitable for multi-resolution full screen filling applications

目标检测领域必看的6篇论文

Six papers that must be seen in the field of target detection

consul安装与配置

boost官网搜索引擎项目详解
随机推荐
Advanced database technology learning notes 1 -- Oracle deployment and pl/sql overview
Xiaoshuidi 2.0 website navigation network template
目标检测领域必看的6篇论文
Unity鼠标带动物体运动的三种方法
How to effectively implement a rapid and reasonable safety evacuation system in hospitals
P5472 [NOI2019] 斗主地(期望、数学)
「以云为核,无感极速」第五代验证码重磅来袭
How to use JWT for authentication and authorization
Consumer installation and configuration
Flash point list of cross platform efficiency software
How to deal with invalid objects monitored by Oracle every day in the production environment?
R language - some metrics for unbalanced data sets
AlexNet—论文分析及复现
一种比读写锁更快的锁,还不赶紧认识一下
【MySQL】Got an error reading communication packets
[half understood] zero value copy
Random talk on GIS data (V) - geographic coordinate system
Why does acid food hurt teeth + early periodontitis
Top ten application development trends from 2022 to 2023
Sirius network verification source code / official genuine / included building tutorial