当前位置:网站首页>async await如何实现并发
async await如何实现并发
2022-07-28 11:10:00 【卷王出征】
还记得去年实习参加秋招,二面面试官龙哥(现在的组长)问了一个面试题,async await如何实现并发?今晚突然又想起来,赶紧整理一下。
举例
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)
}
})
})
}
//继发读取
async function ReadTwo(){
var f1=await read('./a.txt');
var f2=await read('./b.txt');
}
解决方案
(1) promise.all()
async function readTwo{
var p=Promise.all([read('./a.txt'),read('./b.txt')]);
var [f1,f2]=await p
}
因为promise.all是并发,所以这两个请求真正达到并发
(2)没法描述,看代码
async function readTwo{
var p1=read('./a.txt');
var p2=read('./a.txt');
f1=await p1;
f2=await p2;
}
这两个请求真的是并发,因为它们是异步函数,但是获取结果后的赋值时继发
现在想想好简单,当时都不会。
边栏推荐
- Iterative method for determinant (linear algebraic formula)
- 使用 Terraform 在 AWS 上快速部署 MQTT 集群
- Using C language to realize bidirectional linked list
- Tiktok programmer confession special code tutorial (how to play Tiktok)
- How to use JWT for authentication and authorization
- MySQL离线同步到odps的时候 可以配置动态分区吗
- 小水滴2.0网站导航网模板
- Four advantages of verification code to ensure mailbox security
- AlexNet—论文分析及复现
- Will PFP be the future of digital collections?
猜你喜欢

Introduction to the usage of SAP ui5 image display control avatar trial version

什么是WordPress

Design and implementation of SSM personal blog system

Sirius network verification source code / official genuine / included building tutorial
![[pyGame practice] when the end of the world comes, how long can you live in a cruel survival game that really starts from scratch?](/img/2b/1eb02249ab9ad0b4e1bfeeee87418c.png)
[pyGame practice] when the end of the world comes, how long can you live in a cruel survival game that really starts from scratch?

Install SSL Certificate in Litespeed web server

Jupiter、spyder、Anaconda Prompt 、navigator 快捷键消失的解决办法

Update dev (development version) of the latest win11

对话庄表伟:开源第一课

Good use explosion! The idea version of postman has been released, and its functions are really powerful
随机推荐
Outlook suddenly becomes very slow and too laggy. How to solve it
R language uses oneway The test function performs one-way ANOVA
Understand how to prevent tampering and hijacking of device fingerprints
「以云为核,无感极速」第五代验证码重磅来袭
[FPGA tutorial case 41] image case 1 - reading pictures through Verilog
A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?
Google Earth engine - use geetool to download single scene images in batches and retrieve NDSI results with Landsat 8
Jupiter、spyder、Anaconda Prompt 、navigator 快捷键消失的解决办法
b2子主题/博客b2child子主题/开源源码
PKG packaging node project
万字详解 Google Play 上架应用标准包格式 AAB
Dialogue with Zhuang biaowei: the first lesson of open source
R language uses LM function to build regression model with interactive items, and uses: sign (colon) to represent the interaction of variables (colon is pure multiplication, excluding the constituent
使用c语言实现双向链表
数字孪生轨道交通:“智慧化”监控疏通城市运行痛点
移动端人脸风格化技术的应用
PHP检测url网址链接是否正常可访问
B2 sub theme / blog b2child sub theme / open source code
一种比读写锁更快的锁,还不赶紧认识一下
Excel shortcut keys (letters + numbers) Encyclopedia